需要将视图添加为 TableHeader?

Posted

技术标签:

【中文标题】需要将视图添加为 TableHeader?【英文标题】:Need to add a view as TableHeader? 【发布时间】:2014-04-25 13:04:36 【问题描述】:

是否可以将高度为 672 的视图加载为 TableView 标题?我添加了它但我无法滚动并查看 tableview 单元格详细信息。 我已经使用了代码

ACCRemainderView *header =  [ACCRemainderView customHeaderView];

[self.tableView setTableHeaderView:header];

在剩余视图中

+ (id)customHeaderView

    ACCRemainderView *customView = [[[NSBundle mainBundle] loadNibNamed:@"ACCRemainderView" owner:nil options:
                                     nil] lastObject];

    //make sure customView is not nil or the wrong class!        
    if ([customView isKindOfClass:[ACCRemainderView class]]) 
        return customView;
     else 
        return nil;
    

【问题讨论】:

你的实际问题是什么? 我将 Nib 文件加载为 TableView 标题,但我无法滚动并查看 tableview 详细信息。 如果你的表格只有一个section,尝试使用Section Header 格式精美的代码更容易阅读 :) 【参考方案1】:

尝试在您的UITableViewController 中实现- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 并返回[ACCRemainderView customHeaderView]

【讨论】:

【参考方案2】:

设置tableHeaderView是最好的选择。我测试过,它有效。

ViewForHeaderInSection 不能,因为在此视图标题的高度大于表格的高度。

您不能滚动,我认为您的 tableView 与另一个视图重叠。

- (void)viewDidLoad

[super viewDidLoad];

_tableView.dataSource = self;
_tableView.delegate = self;


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

NSString *title=[self tableView:tableView titleForHeaderInSection:section];
if ( !title )
    return nil;


UIView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 500)];
view.backgroundColor = [UIColor greenColor];
return view;


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

return 500;



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

return 2;


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

return 2;


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

return @"abcd";


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

return 50;



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

cell.textLabel.text = @"a";
return cell;

【讨论】:

以上是关于需要将视图添加为 TableHeader?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 jQuery 导出时将 dataTables 中的所有 tableHeader 值更改为大写

如何为 TableHeader 使用渲染器

如何将 UITableviewcell 子视图添加为 uiscrollview

React几种函数的写法

WKWebView 添加为子视图

从视图或部分视图将 CSS 或 JavaScript 文件添加到布局头