如何从 UItableView 获取部分标题 [英] How to get section header from UItableView

查看:74
本文介绍了如何从 UItableView 获取部分标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get section header view using this code:

[tableView headerViewForSection:indexPath.section];

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

but this code always returns me nil. Could you give me some examples to get section header view from table view? This is my viewForHeader implementation:

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

推荐答案

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

Quite old question, but maybe a solution is helpful to others too...

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

An UITableView does create cell, header and footer views only if they are needed (e.g. they are visible in the table view content area).

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

If the cell, header or footer view isn't visible in the table view, a call to 'cellForRowAtIndexPath:', 'headerViewForSection:' or 'footerViewForSection:' might return 'nil'. (See UITableView.h for documentation of this behaviour. The only exception would be, that the view has not jet been recycled by the table view)

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

Of course you can create any table view subview by calling the responsible delegate method directly, but UITableView wouldn't do so by itself.

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

So the solution to access a cell, header oder footer view is to make it visible in the table view first.

标题视图示例:

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

这篇关于如何从 UItableView 获取部分标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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