从ViewController调用UITableViewDelegate方法

时间:2011-10-05 19:31:15

标签: ios uitableview

我想从我的视图控制器类中调用以下方法:

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

然而,对于我的生活,我无法弄清楚如何去做。我试过了:

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

所有人都给我错误。这是开头的额外tableView:位。任何人都可以提出一些建议或至少解释tableView:(UITableView *)tableView的含义吗?

谢谢! 史蒂夫

2 个答案:

答案 0 :(得分:6)

我不知道您为什么要调用它,但是如果它是在您从那里调用它的同一个对象中实现的,那么您可以使用self

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

否则,您可以从tableView获取委托并调用委托:

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

答案 1 :(得分: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属性的方法。

Apples文档说如果使用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设置标题高度,那么您应该已经在程序中计算了高度。设置后,我认为您不能查询特定部分(或行)的高度。

这有帮助吗?