如何使用UITableViewHeaderFooterView? [英] How to use UITableViewHeaderFooterView?

查看:647
本文介绍了如何使用UITableViewHeaderFooterView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想在我的应用中使用 UITableHeaderFooterView ,我这样做:

Hi I want to use UITableHeaderFooterView in my app and i am doing this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [_tableView registerClass:[M3CTableViewCell class] forCellReuseIdentifier:@"cell"];
    [_tableView registerClass:[M3CHeaderFooter class] forHeaderFooterViewReuseIdentifier:@"footer"];

}

- (UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section {
    M3CHeaderFooter * footer = [[M3CHeaderFooter alloc]initWithReuseIdentifier:@"footer"];
    footer.textLabel.text = @"Test";
    return footer;
}

通过这样做我在Footer的地方得不到任何东西。
这个方法甚至没有调用,但我认为这个方法是 UITableViewDelegate 协议的一部分。

By doing this I am not getting anything at Footer's place. And this method is not even getting called but I think this method is part of UITableViewDelegate protocol.

推荐答案

使用可重复使用的页眉/页脚视图的新iOS 6功能包含两个步骤。你好像只是第一步。

Using the new iOS 6 feature of reusable header/footer views involves two steps. You seem to be doing only the first step.

第一步:通过注册自定义子类,告诉表查看用于节头视图的类UITableViewHeaderFooterView(我假设您的M3CHeaderFooter是UITableViewHeaderFooterView的子类)。

First step: you're telling the table view what class to use for the section header view, by registering your custom subclass of UITableViewHeaderFooterView (I assume your M3CHeaderFooter is a subclass of UITableViewHeaderFooterView).

第二步:通过实现tableView委托方法告诉表视图对标题部分使用什么视图(AND重用)

Second step: Tell the table view what view to use (AND reuse) for a header section by implementing the tableView delegate method

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

所以在你的viewDidLoad中你实现了这样的东西:

So in your viewDidLoad you'd implement something like this:

    // ****** Do Step One ******
    [_tableView registerClass:[M3CHeaderFooter class] forHeaderFooterViewReuseIdentifier:@"TableViewSectionHeaderViewIdentifier"];

然后,您将在创建和显示您的类中实现表查看委托方法表视图:

Then you'd implement the table View delegate method in the class where you're creating and displaying your table view:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *headerReuseIdentifier = @"TableViewSectionHeaderViewIdentifier";

    // ****** Do Step Two *********
    M3CHeaderFooter *sectionHeaderView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerReuseIdentifier];
   // Display specific header title
   sectionHeaderView.textLabel.text = @"specific title";   

    return sectionHeaderView;    
}

现在请注意,您不需要为了使用它而将UITableViewHeaderFooterView子类化。
在iOS 6之前,如果你想要一个部分的标题视图,你将实现上面的tableView委托方法并告诉表视图每个部分使用什么视图。所以每个部分都有一个你提供的UIView的不同实例。这意味着如果你的tableView有100个部分,并且在delegate方法中你创建了一个UIView的新实例,那么你就可以为显示的100个部分标题提供tableView 100 UIViews。

Now mind you that you do not need to subclass UITableViewHeaderFooterView in order to use it. Before iOS 6, if you wanted to have a header view for a section, you'd implement the above tableView delegate method and tell the table view what view to use for each section. So each section had a different instance of a UIView which you provided. This means that if your tableView had 100 sections, and inside the delegate method you created a new instance of a UIView, then you would have given the tableView 100 UIViews for the 100 section headers that were displayed.

使用可重复使用的页眉/页脚视图的新功能,您可以创建UITableViewHeaderFooterView的实例,系统会为每个显示的节标题重复使用它。

Using the new feature of reusable header/footer views, you create an instance of a UITableViewHeaderFooterView and the system reuses it for each displayed section header.

如果你想拥有一个没有子类化的可重复使用的UITableViewHeaderFooterView,那么你只需将viewDidLoad更改为:

If you wanted to have a reusable UITableViewHeaderFooterView without subclassing then you simply change your viewDidLoad to this:

// Register the class for a header view reuse.
[_buttomTableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"TableViewSectionHeaderViewIdentifier"];

然后你的委托方法:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *headerReuseIdentifier = @"TableViewSectionHeaderViewIdentifier";

   // Reuse the instance that was created in viewDidLoad, or make a new one if not enough.
    UITableViewHeaderFooterView *sectionHeaderView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerReuseIdentifier];
    sectionHeaderView.textLabel.text = @"Non subclassed header";

    return sectionHeaderView;

}

我希望这很清楚。

编辑:在对标题视图进行子类化时,如果要向headerView添加自定义视图,可以实现类似以下的代码:

When subclassing the header view, you can implement code similar to the following if you wish to add a custom view to the headerView:

        // Add any optional custom views of your own
    UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 50.0, 30.0)];
    [customView setBackgroundColor:[UIColor blueColor]];

    [sectionHeaderView.contentView addSubview:customView];

在子类中执行此操作,而不是使用viewForHeaderInSection:delegate方法(如Matthias所述),将确保只创建任何子视图的一个实例。然后,您可以在子类中添加任何属性,以允许您访问自定义子视图。

Doing this in the subclass, as opposed to viewForHeaderInSection: delegate method (as noted below by Matthias), will ensure that only one instance of any subviews are created. You can then add any properties within the subclass that will allow you to access your custom subview.

这篇关于如何使用UITableViewHeaderFooterView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆