如何使用Storyboard实现自定义表视图节页眉和页脚 [英] How to Implement Custom Table View Section Headers and Footers with Storyboard

查看:164
本文介绍了如何使用Storyboard实现自定义表视图节页眉和页脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果不使用故事板,我们只需将 UIView 拖到画布上,然后将其放置在 tableView:viewForHeaderInSection tableView:viewForFooterInSection 委托方法。

Without using a storyboard we could simply drag a UIView onto the canvas, lay it out and then set it in the tableView:viewForHeaderInSection or tableView:viewForFooterInSection delegate methods.

我们如何使用StoryBoard我们无法将UIView拖到画布上

推荐答案

只需使用原型单元作为节标题和/或页脚。

Just use a prototype cell as your section header and / or footer.


  • 添加一个额外的单元格并将所需的元素放入其中。

  • 将标识符设置为特定的(在我的情况下为SectionHeader)

  • 实现 tableView:viewForHeaderInSection:方法或 tableView:viewForFooterInSection:方法

  • 使用 [tableView dequeueReusableCellWithIdentifier:] 获取标题

  • 实现 tableView:heightForHeaderInSection:方法。

  • add an extra cell and put your desired elements in it.
  • set the identifier to something specific (in my case SectionHeader)
  • implement the tableView:viewForHeaderInSection: method or the tableView:viewForFooterInSection: method
  • use [tableView dequeueReusableCellWithIdentifier:] to get the header
  • implement the tableView:heightForHeaderInSection: method.

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

编辑:如何更改标题标题(评论问题):

How to change the header title (commented question):


  1. 向标题单元格添加标签

  2. 设置标签的标签到特定的数字(例如123)

  3. tableView:viewForHeaderInSection:方法中通过调用获取标签:

  1. Add a label to the header cell
  2. set the tag of the label to a specific number (e.g. 123)
  3. In your tableView:viewForHeaderInSection: method get the label by calling:



    UILabel *label = (UILabel *)[headerView viewWithTag:123]; 




  1. 现在您可以使用标签来设置新标题:



    [label setText:@"New Title"];

这篇关于如何使用Storyboard实现自定义表视图节页眉和页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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