【问题标题】:Add custom header to UITableView using Storyboard使用 Storyboard 向 UITableView 添加自定义标题
【发布时间】:2013-01-20 21:41:26
【问题描述】:

我似乎无法使用 Storyboard 向 UITableView 添加标题。我有一个 UITableView ,其中有一些原型单元格正在显示并且工作正常。然后,我在这些原型单元格上方拖动了一个新的 UIView 并向其添加了一个标签以充当我的表格的标题(而不是作为部分标题)。我创建了一个新的 UIView 子类,它只有一个属性,即 UILabel。 storyboard中UIView的class设置为这个自定义UIView,UILabel的引用outlet设置为我自定义UIView类的UILabel属性。

然后,在我的 UITableViewController 的 viewDidLoad 方法中,我正在执行以下操作:

DetailTableHeaderView *headerView = [[DetailTableHeaderView alloc] init];
headerView.entryNameLabel.text = @"TEST";
self.tableView.tableHeaderView = headerView;
[self.tableView reloadData];

但是当我运行我的应用程序时,表格标题根本没有出现。我还注意到 headerView.entryNameLabel 的 text 属性甚至没有设置为应有的“TEST”。

我在这里做错了什么?

【问题讨论】:

  • 情节提要如何知道要为 tableview 标题加载什么?您只是在初始化视图控制器,但没有与之配套的视图!
  • 不知道你到底是什么意思。在情节提要中,我添加了一个带有 UILabel 的 UIView 作为表头。然后,我创建了一个名为“DetailTableHeaderView”的自定义 UIView 子类,其中包含一个名为“entryNameLabel”的 UILabel 属性。在情节提要中,我将标题视图的类设置为“DetailTableHeaderView”,并链接了 UILabel 引用出口到 UILabel 'entryNameLabel'。这还不够吗?
  • 情节提要如何知道要加载什么视图?您没有告诉它执行任何 segues,从您的评论来看,我敢打赌有问题的 view 没有任何链接,它只是出现在情节提要中。尝试:[self.storyboard instantiateViewControllerWithIdentifier:@"DetailTableHeaderView"]; 设置您的UITableViewHeader。确保DetailTableHeaderViewUIViewController
  • 但是标题视图不能是视图控制器。已经有一个 UITableViewController 并且您不能将第二个视图控制器添加到同一个窗口。故事板知道要加载什么,因为我已经在原型单元格上方拖动了一个新的 UIView,并且我已经将这个新的 UIView 的类属性设置为我以编程方式创建的 UIView 子类。这就是它通常的做法,它适用于其他一切。出于某种原因,这不适用于表格标题...
  • 您在设置标题视图时使用视图控制器view 属性。只需保留对视图控制器的引用,就可以了。原型单元格不同,因为它们是 UITableView 的属性,而不是情节提要。

标签: iphone uitableview header


【解决方案1】:

老问题,但我提供我的答案是出于参考原因,因为如何添加 tableview 标题仍然有点不重要,使用故事板作为设计部分。

  1. 在情节提要的表格视图中添加原型单元格。
  2. 选择原型单元格后,在右侧的属性检查器中为其指定一个标识符(例如 headerViewCell),因为这是您引用它以便使用它的方式。
  3. 现在单击尺寸检查器选项卡并为其指定行高(这将是您的标题视图高度)
  4. 现在在代码中,在处理 tableview 的控制器中:

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.tableView.tableHeaderView = [self.tableView dequeueReusableCellWithIdentifier:@"headerViewCell"];
    }
    

【讨论】:

    【解决方案2】:

    这个问题已经很老了,但写下这个答案希望对某人有所帮助。

    处理表 headerView 的好方法是使用委托方法。实现tableView:viewForHeaderInSectiontableView:heightForHeaderInSectiondelegate 方法。从tableView:viewForHeaderInSectionmethod 返回您的 UIView。

    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    }
    
    -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    }
    

    另一种选择是使用原型单元格(自定义单元格)作为标题视图并在 tableView:viewForHeaderInSection 方法中返回它。请参见下面的代码(我没有测试过这个):

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
    {
    
        HeaderView *headerView = [self.TableView dequeueReusableHeaderFooterViewWithIdentifier:@"tableHeader"];
    
        // Set Text
        headerView.headerLabel.text = @"Some title";
    
        return headerView.contentView;
    }
    

    更新

    上述方法也适用于 tableView:viewForHeaderInSection: 方法。这是示例代码:

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UITableViewCell *sectionHeader;
        CustomSectionHeaderCell *sectionHeaderCell = [tableView dequeueReusableCellWithIdentifier:@"sectionHeaderCell"];
    
        // do stuff here
    
        return sectionHeaderCell.contentView;
    }
    

    【讨论】:

    • 你没有回答这个问题。这不是他想要的。
    • 表头视图和表 section 表头视图之间存在很多混淆。一个表视图只能有一个表头视图,它始终位于表视图的顶部。一个表视图可以有多个节标题视图,这些视图显示在与其关联的节的顶部。
    猜你喜欢
    • 2019-05-22
    • 1970-01-01
    • 2015-09-03
    • 1970-01-01
    • 2012-03-25
    • 2016-05-02
    • 2013-03-14
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多