gpt4 book ai didi

ios - 如何使用 UITableViewHeaderFooterView?

转载 作者:IT老高 更新时间:2023-10-28 11:36:19 28 4
gpt4 key购买 nike

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

- (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;
}

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

最佳答案

使用可重用页眉/页脚 View 的新 iOS 6 功能涉及两个步骤。你似乎只做了第一步。

第一步:您通过注册 UITableViewHeaderFooterView 的自定义子类(我假设您的 M3CHeaderFooter 是 UITableViewHeaderFooterView 的子类)来告诉表格 View 将哪个类用于节标题 View 。

第二步:通过实现 tableView 委托(delegate)方法,告诉 TableView 要对标题部分使用(和重用)什么 View

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

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

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

然后您将在创建和显示表格 View 的类中实现表格 View 委托(delegate)方法:

- (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 之前,如果你想要一个部分的标题 View ,你需要实现上面的 tableView 委托(delegate)方法并告诉 TableView 为每个部分使用什么 View 。因此,每个部分都有您提供的 UIView 的不同实例。这意味着如果您的 tableView 有 100 个部分,并且在委托(delegate)方法中创建了一个 UIView 的新实例,那么您将为显示的 100 个部分标题为 tableView 提供 100 个 UIView。

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

如果您想拥有一个可重用的 UITableViewHeaderFooterView 而无需子类化,那么您只需将 viewDidLoad 更改为:

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

然后是你的委托(delegate)方法:

- (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;

}

我希望这已经足够清楚了。

编辑:在子类化标题 View 时,如果您希望向 headerView 添加自定义 View ,可以实现类似于以下的代码:

        // 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 所述),将确保仅创建任何 subview 的一个实例。然后,您可以在子类中添加任何允许您访问自定义 subview 的属性。

关于ios - 如何使用 UITableViewHeaderFooterView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12900446/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com