如何从UItableView获取节头

Kis*_*der 12 objective-c uitableview ios

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

[tableView headerViewForSection:indexPath.section];
Run Code Online (Sandbox Code Playgroud)

但是这段代码总是让我失败.你能给我一些例子来从表视图中获取节头视图吗?这是我的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];
}
Run Code Online (Sandbox Code Playgroud)

Lab*_*Ars 7

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

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
});
Run Code Online (Sandbox Code Playgroud)


Nik*_*ita 2

你实施了吗

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
Run Code Online (Sandbox Code Playgroud)

在你的 ViewController 子类中?

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