为什么要打电话给它?这是UITableViewDelegate方法之一,通常在tableView构造表时自动调用它。 当调用此方法时,tableView类对象会填充它需要的参数。视图控制器只需提供由您自定义的正确的委托方法,以便它可以正确设置它。
您是否在代理类中自定义代码(如本例中的代码)?
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)];
// create the button object
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor blackColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont boldSystemFontOfSize:20];
headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 44.0);
// If you want to align the header text as centered
// headerLabel.frame = CGRectMake(150.0, 0.0, 300.0, 44.0);
headerLabel.text = <Put here whatever you want to display> // i.e. array element
[customView addSubview:headerLabel];
return customView;
}
我不在我的Mac附近,或者我会给你一个我自己的例子。但这是使用这种方法的一般方法。
顺便说一句,你可以看到参数的tableView没有示例代码引用上方。如果您确实想调用此方法,请使用nil。 UITableViewDelegate协议允许控制器为多个tableView进行委托。如果发生这种情况,该方法应测试以查看哪个tableView被引用,以便为每个tableView调整专门的行为。
附加信息:
如果你只是想看看你的tableView的标题的高度是什么,你可以评估其sectionHeaderHeight财产。还有像这样的其他属性,如sectionFooterHeight和rowHeight。
您应该知道委托方法可以帮助您使用自定义的tableView。所以委托方法tableView:heightForHeaderInSection:实际上是为了自定义标题高度。你的委托方法告诉tableView高度是多少。这不是检查tableView属性的方法。
苹果文档说,如果您使用tableView:heightForHeaderInSection进行自定义:那么tableView sectionHeaderHeight无效。你会期望这一点,因为该属性指的是所有节标题的高度。
使用sectionHeaderHeight属性,您可以在这种情况下写入,您可以将所有标题设置为相同的高度。
下面是我正在处理的一些示例代码。我为你添加了一条NSLog语句。
resultsTableVC = [[[ResultsTableVC alloc] initWithController:self] retain];
self.tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, 110, self.view.frame.size.width, self.view.frame.size.height-120) style:UITableViewStyleGrouped] retain];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.tableView.autoresizesSubviews = YES;
self.tableView.layer.cornerRadius = 10.0f;
self.tableView.delegate = resultsTableVC;
self.tableView.dataSource = resultsTableVC;
self.tableView.backgroundView = nil;
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.separatorColor = [UIColor defaultResultTableBackgroundColor];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
[self.view addSubview:self.tableView];
NSLog(@"header: %f, row: %f", tableView.sectionHeaderHeight, tableView.rowHeight);
(有人可能会指出,我不需要他们有些保留。我还在就这一工作。)
这告诉我标准部分的高度是100,标准的行高是44.0。我有一个指向我的tableView的指针,我可以通过这个类使用一个属性。
现在,如果您使用tableView:heightForHeaderInSection设置标题高度,那么您应该已经在程序中计算了高度。一旦设置好后,我认为您不会查询特定部分(或行)的高度。
这有帮助吗?
感谢您的回复!它必须与heightForHeaderInSection相关。该方法的默认实现不起作用,而不是重复我的逻辑流程,我想获得viewForHeaderInSection的结果,然后根据框架计算高度(如果有的话)。合理? –
调用它的另一个原因是用于测试目的。 –