本文介绍了在InterfaceBuilder中的UITableViewHeaderFooterView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用IB在单独的.xib中创建自定义头部分视图



然而在IB中我找不到一个组件UITableViewHeaderFooterView(类似于UITableViewCell)



我创建了一个类MySection,它继承了从UITableViewHeaderFooterView
创建一个.xib,MySection.xib
注册xib以进行单元重用



问题是如何使用initWitReuseIdentifier ... 。

解决方案

这是绝对可能的,Apple提供了一个示例。



下载示例代码并查看SectionHeaderView.xib。



这样做的方法是创建一个具有单个UIView的xib。然后,将类类型设置为从UITableViewHeaderFooterView继承的类。



一旦您有一个继承自UITableViewHeaderFooterView的类的nib,调用以下注册类重复使用为页眉或页脚:

  static NSString * const SectionHeaderViewIdentifier = @SectionHeaderViewIdentifier; 

[self.tableView registerNib:[UINib nibWithNibName:@SectionHeaderViewbundle:nil] forHeaderFooterViewReuseIdentifier:SectionHeaderViewIdentifier];

要使用视图,请实现table delegate方法tableView:viewForHeaderInSection: p>

   - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSinteger)section {

SectionHeaderViewClass * sectionHeaderView =(SectionHeaderView *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:SectionHeaderViewIdentifier];

// Do stuff ...

return sectionHeaderView;
}


I want to create a custom header section view in a separate .xib using IB

However in IB i cannot find a component UITableViewHeaderFooterView (similar to UITableViewCell) and assign a reuse for it

So how to create in custom header section?

I create a class MySection which inherits from UITableViewHeaderFooterViewCreate a .xib, MySection.xibRegister xib for cell reuse

the problem is, how to use the initWitReuseIdentifier....

解决方案

It's absolutely possible and Apple provides an example here.

Download the sample code and look at the SectionHeaderView.xib.

The way to do it is to create a xib with a single UIView on it. Then, set the class type to a class that inherits from UITableViewHeaderFooterView.

Once you have a nib with a class that inherits from UITableViewHeaderFooterView, call the following to register the class for reuse as a header or footer:

static NSString * const SectionHeaderViewIdentifier = @"SectionHeaderViewIdentifier";

[self.tableView registerNib:[UINib nibWithNibName:@"SectionHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:SectionHeaderViewIdentifier];

To put the view into use, implement the table delegate method tableView:viewForHeaderInSection: like so:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSinteger)section {

    SectionHeaderViewClass *sectionHeaderView = (SectionHeaderView *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:SectionHeaderViewIdentifier];

    // Do stuff...

    return sectionHeaderView;
}

这篇关于在InterfaceBuilder中的UITableViewHeaderFooterView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 17:38