2011-10-05 68 views
2

一个的UITableViewDelegate方法,我想从我的视图控制器类调用下面的方法:拨打国际长途的ViewController

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

但是我的生活中,我无法弄清楚如何做到这一点。我试过:

所有给我的错误。这是多余的tableView:位。任何人都可以提供一些建议或至少解释tableView:(UITableView *)tableView是什么意思?

谢谢! Steve

回答

0

为什么要打电话给它?这是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设置标题高度,那么您应该已经在程序中计算了高度。一旦设置好后,我认为您不会查询特定部分(或行)的高度。

这有帮助吗?

+0

感谢您的回复!它必须与heightForHeaderInSection相关。该方法的默认实现不起作用,而不是重复我的逻辑流程,我想获得viewForHeaderInSection的结果,然后根据框架计算高度(如果有的话)。合理? –

+0

调用它的另一个原因是用于测试目的。 –

6

我不知道为什么你会怎么称呼它,但如果它是你是从调用它同一个对象实现,那么你可以使用self

UIView* view = [self tableView:tableView viewForHeaderInSection:section]; 

否则,你可以从tableView中获得委托并致电委托:

id<UITableViewDelegate> theDelegate = tableView.delegate; 
UIView* view = [theDelegate tableView:tableView viewForHeaderInSection:section];