【问题标题】:How to get section header from UItableView如何从 UItableView 获取节标题
【发布时间】:2014-01-02 12:04:04
【问题描述】:

我正在尝试使用此代码获取节标题视图:

[tableView headerViewForSection:indexPath.section];

但是这段代码总是返回零。 你能给我一些例子来从表格视图中获取部分标题视图吗? 这是我的 viewForHeader 实现:

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

  DynamicHeader *headerView = [[DynamicHeader alloc] init];
        headerView.frame = CGRectMake(0, 0, 320, 40);
        UILabel *headerLbl = [[UILabel alloc] init];
        headerLbl.frame = CGRectMake(10, 10, 300, 20);
        if(((SectionViewController *)sharedInstance.currentViewController).currentSectionType == 25)
        {
            UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"minus.png"]];
            imgView.frame = CGRectMake(285, 14.5, 16, 13);
            [headerView addSubview:imgView];
            UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:sharedInstance action:@selector(headerTap:)];
            [headerView addGestureRecognizer:recognizer];
            headerView.tableView = tableView;
            headerView.heightForRows = 95;
            headerView.isOpen = YES;
        }
 headerLbl.text = [[[[[((SectionViewController *)sharedInstance.currentViewController).responseDictionary valueForKey:DATA_PARAMETER] valueForKey:SECTION_TABLE_PARAMETER] objectAtIndex:section] valueForKey:TABLE_SECTION_HEADER] capitalizedString];
 [headerView addSubview:headerLbl];

        return headerView;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

 if((DynamicHeader *)[tableView headerViewForSection:indexPath.section] != nil)
        {
            return ((DynamicHeader *)[tableView headerViewForSection:indexPath.section]).heightForRows;
        }
        else
        {
            return 95;
        }

}


- (void)headerTap: (UITapGestureRecognizer *)recognizer
{
    DynamicHeader *view = (DynamicHeader *)[recognizer view];
    if(view.isOpen)
    {
        view.heightForRows = 0;

    }
    else
    {
        view.heightForRows = 95;
    }
    view.isOpen = !view.isOpen;

    [view.tableView beginUpdates];
    [view.tableView endUpdates];
}

【问题讨论】:

  • 你是否使用定义的 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 来分配视图?
  • 你对headerViewForSection:的实现是什么(如果有的话)?
  • @rokjarc 我更新了我的问题。
  • 在这种情况下,您的 tableview 对象在您调用此方法时似乎是 nil。或者它的delegate 尚未设置(由您)。因为tableview 调用它的delegate 方法 - 它没有实现自己的。
  • @rokjarc,目前我正在调用此方法 tableview 不是零。另外,我设置了 tableview 委托和数据源。

标签: ios objective-c uitableview


【解决方案1】:

相当老的问题,但也许一个解决方案对其他人也有帮助......

只有在需要时,UITableView 才会创建单元格、页眉和页脚视图(例如,它们在表格视图内容区域中可见)。

如果单元格、页眉或页脚视图在表格视图中不可见,则调用“cellForRowAtIndexPath:”、“headerViewForSection:”或“footerViewForSection:' 可能会返回 'nil'。 (有关此行为的文档,请参阅 UITableView.h。唯一的例外是,视图尚未被表视图回收)

当然你可以通过直接调用负责的委托方法来创建任何表格视图子视图,但 UITableView 不会自己这样做。

所以访问单元格、页眉或页脚视图的解决方案是首先使其在表格视图中可见。

标题视图示例:

CGRect  sectionHeaderRect = [self.tableView rectForHeaderInSection:groupSectionIndex];
[self.tableView scrollRectToVisible:sectionHeaderRect
                           animated:NO];

// Let the tableview load the needed views
dispatch_async(dispatch_get_main_queue(), ^{

    UITableViewHeaderFooterView*    groupSectionHeaderView = [self.tableView headerViewForSection:sectionIndex];

    // Use groupSectionHeaderView
});

【讨论】:

  • 在我的情况下,尽管标题可见且索引正确,但它始终返回 nil。
【解决方案2】:

使用此委托方法访问标题视图:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section

【讨论】:

    【解决方案3】:

    你实现了吗

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

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
    

    在您的 ViewController 子类中?

    如果是,那么您可能在其中一种方法中遇到了问题。

    【讨论】:

    • @KiselAlexander,调用 cellForRowAtIndexPath 是否也返回 nil?
    • 所以看看你对viewForHeaderInSection的实现,我想,问题就在那里。你在运行时看到标题了吗?
    • nik,我应该在 viewForHeaderInSection 中使用 dequeueReusableHeaderFooterViewWithIdentifier 吗?
    【解决方案4】:

    您的标题视图必须从 UITableViewHeaderFooterView 继承,否则此调用将不起作用并始终返回 nil。

    【讨论】:

      【解决方案5】:

      你在哪里打电话给[tableView headerViewForSection:indexPath.section];

      如果您在 tableView 可见之前在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 中调用它,那么您将返回 nil,因为 tableView 尚未设置。

      如果你在加载 tableView 后在其他地方调用它,你应该得到一个 UITableViewHeaderFooterView 返回。

      【讨论】:

      • 我正在调用 [tableView headerViewForSection:indexPath.section];当 tableview 已经可见时,在 heightForRowAtIndexPath 中。
      • 是的,在 heightForRow 中调用它也会为我返回 nil。我怀疑在表格视图计算单元格高度时没有设置/刷新标题。但是,它不适用于单元格触摸事件。如果您有自定义标题视图,也许您可​​以为其创建一个 IBOutlet?
      【解决方案6】:

      heightForRowAtIndexPathviewForHeaderInSection 之前调用,所以你当然会得到nil

      【讨论】:

      • 我多次调用 heightForRowAtIndexPath。一次当我创建 tableview 时,然后当我调用 [tableview beginUpdates] 时。在第二种情况下,我已经有了部分的标题。
      • 请问在heightForRowAtIndexPath中调用headerViewForSection需要什么?
      • 我有自定义部分标题视图,其中包含此部分中所有单元格的高度。我不想在我的主类中有额外的变量,所以我在标题视图中保持高度。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-21
      • 1970-01-01
      • 2013-07-10
      • 1970-01-01
      • 2013-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多