不使用Storyboard,我们可以在画布上拖动一个UIView
,进行布局,然后在tableView:viewForHeaderInSection
或tableView:viewForFooterInSection
委托方法中设置它。
如果使用Storyboard,我们该怎么做呢?因为我们无法在画布上拖动一个UIView。
不使用Storyboard,我们可以在画布上拖动一个UIView
,进行布局,然后在tableView:viewForHeaderInSection
或tableView:viewForFooterInSection
委托方法中设置它。
如果使用Storyboard,我们该怎么做呢?因为我们无法在画布上拖动一个UIView。
只需将原型单元格用作您的部分标题和/或页脚。
tableView:viewForHeaderInSection:
方法或tableView:viewForFooterInSection:
方法[tableView dequeueReusableCellWithIdentifier:]
获取标题tableView:heightForHeaderInSection:
方法。-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
static NSString *CellIdentifier = @"SectionHeader";
UITableViewCell *headerView = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (headerView == nil){
[NSException raise:@"headerView == nil.." format:@"No cells with matching CellIdentifier loaded from your storyboard"];
}
return headerView;
}
编辑:如何更改标题(评论问题):
tableView:viewForHeaderInSection:
方法中,通过调用以下代码获取标签: UILabel *label = (UILabel *)[headerView viewWithTag:123];
[label setText:@"New Title"];
我知道这个问题是关于iOS 5的,但为了日后读者的利益,请注意从iOS 6开始,我们现在可以使用dequeueReusableHeaderFooterViewWithIdentifier
代替dequeueReusableCellWithIdentifier
。
所以在viewDidLoad
中,调用registerNib:forHeaderFooterViewReuseIdentifier:
或registerClass:forHeaderFooterViewReuseIdentifier:
。然后在viewForHeaderInSection
中,调用tableView:dequeueReusableHeaderFooterViewWithIdentifier:
。您不会使用此API的单元格原型(它是基于NIB的视图或通过编程创建的视图),但这是用于出列头部和尾部的新API。
dequeueReusableHeaderFooterViewWithIdentifier
,而可以使用它来编写代码,即使用特定于标题和页脚的单元格,而不是常规单元格? - mfaaniiOS 6.0及以上版本,使用新的dequeueReusableHeaderFooterViewWithIdentifier
API时有所变化。
我写了一篇指南(在iOS 9上测试过),可以总结如下:
UITableViewHeaderFooterView
viewDidLoad
中注册NIBviewForHeaderInSection
并使用dequeueReusableHeaderFooterViewWithIdentifier
获取头部/尾部我使用Storyboard中的原型单元格在iOS7中使其工作。我的自定义节头视图中有一个按钮,触发了在Storyboard中设置的segue。
首先看一下Tieme的解决方案
正如pedro.m所指出的那样,问题在于点击分区标题会导致选择该分区中的第一个单元格。
正如Paul Von所指出的那样,通过返回单元格的contentView而不是整个单元格来解决这个问题。
但是,正如Hons所指出的那样,长按该节标题将导致应用崩溃。
解决方法是从contentView中删除任何gestureRecognizers。
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
static NSString *CellIdentifier = @"SectionHeader";
UITableViewCell *sectionHeaderView = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
while (sectionHeaderView.contentView.gestureRecognizers.count) {
[sectionHeaderView.contentView removeGestureRecognizer:[sectionHeaderView.contentView.gestureRecognizers objectAtIndex:0]];
}
return sectionHeaderView.contentView; }
如果您在部分页眉视图中没有使用手势,这个小技巧似乎可以解决问题。
如果您需要Swift实现,请按照被接受的答案中的说明进行操作,然后在您的UITableViewController中实施以下方法:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return tableView.dequeueReusableCell(withIdentifier: "CustomHeader")
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 75
}
我想到的解决方案基本上与引入storyboard之前使用的解决方案相同。
创建一个新的、空的接口类文件。将UIView拖到画布上,按需要进行布局。
手动加载nib,在viewForHeaderInSection或viewForFooterInSection委托方法中分配给适当的标题/页脚部分。
我曾希望苹果公司通过storyboard简化这种情况,并继续寻找更好或更简单的解决方案。例如,自定义表头和表尾很容易添加。
tableView:titleForHeaderInSection
,这只需要一行代码。 - John Starr Dewar表头/表尾的包装类。 它只是一个容器,从UITableViewHeaderFooterView继承,并在其中保存单元格。
https://github.com/Magnat12/MGTableViewHeaderWrapperView.git
在您的UITableView中注册类(例如,在viewDidLoad中)
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[MGTableViewHeaderWrapperView class] forHeaderFooterViewReuseIdentifier:@"ProfileEditSectionHeader"];
}
在您的UITableView委托中:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
MGTableViewHeaderWrapperView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"ProfileEditSectionHeader"];
// init your custom cell
ProfileEditSectionTitleTableCell *cell = (ProfileEditSectionTitleTableCell * ) view.cell;
if (!cell) {
cell = [tableView dequeueReusableCellWithIdentifier:@"ProfileEditSectionTitleTableCell"];
view.cell = cell;
}
// Do something with your cell
return view;
}
[NSNull null]
填充表头/表尾
viewForHeaderInSection
方法中返回cell.contentView
即可(无需添加任何自定义UIView)。 - paulvsdequeueReusableHeaderFooterViewWithIdentifier
方法,现在比这个答案更受欢迎。但是,正确使用它现在需要更多步骤。我有一个指南@ http://samwize.com/2015/11/06/guide-to-customizing-uitableview-section-header-footer/。 - samwizeUITableViewCell
用作表头视图。这样做会导致此类视觉故障非常难以调试 - 由于单元格的重用,表头有时会消失,您可能需要花费数小时才能找到问题所在,直到意识到UITableViewCell
不适用于UITableView
表头。 - raven_raven