UITableView -headerViewForSection返回(null) [英] UITableView -headerViewForSection returns (null)

查看:91
本文介绍了UITableView -headerViewForSection返回(null)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableView ,有2个部分。每个都有它自己的 headerView

我已经通过<$ c创建了一个自定义 headerView $ c> -viewForHeaderInSection:方法。

后来,我打算稍微修改它,所以我需要使用 viewForHeader 方法,但我无法访问 headerView ,它是 subViews

I have a UITableView with 2 sections. Each with it's own headerView.
I have created a custom headerView via the -viewForHeaderInSection: method.
Later, I plan to modify it a bit so I need to use the viewForHeader method but I am unable to access the headerView and it's subViews.

作为一个简单的例子,我在<$ c中尝试 NSLog viewForHeader 对象$ c> -didSelectRowAtIndexPath:但我得到(null)结果。

As a simple example, I'm trying to NSLog the viewForHeader object in the -didSelectRowAtIndexPath: but I get a (null) result.

示例代码:

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 75;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{    
    UIView *myHeader = [[UIView alloc] init];
    switch (section) {
        case 0:
            [myHeader setBackgroundColor:[UIColor greenColor]];
            break;
        case 1:
            [myHeader setBackgroundColor:[UIColor redColor]];
            break;
        default:
            break;
    }

    UILabel *myLabel = [[UILabel alloc] init];
    [myLabel setFrame:CGRectMake(10, 0, 100, 30)];
    [myLabel setTag:101];
    [myLabel setBackgroundColor:[UIColor clearColor]];
    [myLabel setText:[NSString stringWithFormat:@"Section: %d",section]];

    [myHeader addSubview:myLabel];    
    return myHeader;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewHeaderFooterView *testView = [self.tableView headerViewForSection:indexPath.section];
    NSLog(@"%@",testView);  //displays (null)
}






我是否需要创建自定义 UIView xib作为 headerView ? (因为根据类似问题并按照文档


Do I need to create the custom UIView xib as the headerView? (because as per a similar question and as per the docs")


要使表格视图能够识别页眉或页脚视图,您需要注册它。

您可以使用registerNib执行此操作:forCellReuseIdentifier:或者
registerClass:forCellReuseIdentifier:UITableView的方法。

To make the table view aware of your header or footer view, you need to register it.
You do this using the registerNib:forCellReuseIdentifier: or
registerClass:forCellReuseIdentifier: method of UITableView.


推荐答案

方法1:



好的,经过一些试验和错误,我终于解决了自己的困境。

我做了 headerView ,就像我做一个单元格一样。

METHOD 1:

Ok, after some trial and error, I finally solved my own dilemma.
I did the headerView just as I would do a cell.

对于一个单元格,我们需要 UITableViewCell 并使用 dequeueReusableCellWithIdentifier

while ...

对于页眉/页脚,我们将使用 UITableViewHeaderFooterView 并使用 dequeueReusableHeaderFooterViewWithIdentifier 方法。

For a cell we would take UITableViewCell and use dequeueReusableCellWithIdentifier
while...
For a Header/Footer, we will take UITableViewHeaderFooterView and use the dequeueReusableHeaderFooterViewWithIdentifier method.

剩下的几乎和单元格一样。

The rest is pretty much the same concept as a cell.


  1. 将标题高度设置为40

  2. 将节数设置为2或更多

  3. 将每个节的行数设置为至少1

  4. iOS6 +( UITableViewHeaderFooterView 不适用于iOS5及以下

  1. Set header height to 40
  2. Set number of sections to 2 or more
  3. Set number of rows per section to be atleast 1
  4. iOS6+ (UITableViewHeaderFooterView won't work with iOS5 and below)






第一种方法:



-viewForHeaderInSection中创建并使用默认的 UITableViewHeaderFooterView code>方法:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *HeaderIdentifier = @"header";

    UITableViewHeaderFooterView *myHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
    if(!myHeader) {
        myHeader = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:HeaderIdentifier];
    }

    UIButton *btnUp = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btnUp setTag:101];
    [btnUp setTitle:@"-" forState:UIControlStateNormal];
    [btnUp setFrame:CGRectMake(tableView.frame.size.width - 35, 5, 30, 30)];
    [myHeader addSubview:btnUp];

    [myHeader.textLabel setText:[NSString stringWithFormat:@"Section: %d",section]];

    [myHeader setFrame:CGRectMake(0, 0, tableView.frame.size.width, 50)];
    return myHeader;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewHeaderFooterView *theHeaderView = [tableView headerViewForSection:indexPath.section];
    NSLog(@"%@",theHeaderView); // -- great! ... not (null) anymore

    UIButton *theButton = (UIButton *)[theHeaderView viewWithTag:101];
    [theButton setTitle:@"+" forState:UIControlStateNormal];
}






第二种方法:



使用自定义 UITableViewHeaderFooterView 子类:


Second Approach:

Using a custom UITableViewHeaderFooterView subclass:


  1. 创建一个 UITableViewHeaderFooterView 子类并将其命名为 CustomHeaderView

  2. 为类创建了一个View接口nib文件

  3. 在xib中,选择了View&在它的身份检查器

    • 指定自定义类 CustomHeaderView

  1. Created a UITableViewHeaderFooterView subclass and named it CustomHeaderView
  2. Created a View interface nib file for the class
  3. In the xib, selected the View & in it's Identity Inspector
    • Specified the Custom Class as CustomHeaderView

  • @property(强,非原子)IBOutlet UILabel * lblSomething;

  • @property(强,非原子)IBOutlet UIButton * btnSomething;

  • @property (strong, nonatomic) IBOutlet UILabel *lblSomething;
  • @property (strong, nonatomic) IBOutlet UIButton *btnSomething;

修改 -viewForHeaderInSection:& -didSelectRowAtIndexPath: as:

Modified the -viewForHeaderInSection: & -didSelectRowAtIndexPath: as:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *HeaderIdentifier = @"header";

    CustomHeaderView *myHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
    if(!myHeader) {
    //    [tableView registerClass:[CustomHeaderView class] forHeaderFooterViewReuseIdentifier:HeaderIdentifier];
        myHeader = [[[NSBundle mainBundle] loadNibNamed:@"CustomHeaderView"
                                                  owner:self
                                                options:nil] objectAtIndex:0];
    }

    [myHeader.btnSomething setTitle:@"-" forState:UIControlStateNormal];
    [myHeader.lblSomething setText:[NSString stringWithFormat:@"Section: %d",section]];

    return myHeader;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomHeaderView *theHeaderView = (CustomHeaderView*)[tableView headerViewForSection:indexPath.section];
    NSLog(@"%@",theHeaderView);

    [theHeaderView.lblSomething setAlpha:theHeaderView.lblSomething.alpha-0.1];
    [theHeaderView.btnSomething setTitle:@"+" forState:UIControlStateNormal];
}






PS: UITableViewHeaderFooterView 的问题在于它只是iOS6 +,如果出于任何原因,你的标题是/必须是 UIView ,然后看下一个方法


PS: The issue with UITableViewHeaderFooterView is that it is iOS6+ only and if, for any reason, your header is/must be a UIView, then see the next method

使用简单的 UIView

Using a simple UIView:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *vwHeader = [[UIView alloc] init];
    [vwHeader setTag:200 + section]; //[1] first method

    switch (section) {
        case 0:
            [vwHeader setBackgroundColor:[UIColor greenColor]];
            break;
        case 1:
            [vwHeader setBackgroundColor:[UIColor redColor]];
            break;
        default:
            break;
    }

    UILabel *lblTitle = [[UILabel alloc] init];
    [lblTitle setFrame:CGRectMake(10, 0, 100, 30)];
    [lblTitle setTag:100 + section]; //[2] alternative method
    [lblTitle setBackgroundColor:[UIColor clearColor]];
    [lblTitle setText:[NSString stringWithFormat:@"Section: %d",section]];

    [vwHeader addSubview:lblTitle];
    return vwHeader;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIView *vwTest = [self.tableView viewWithTag:200 + indexPath.section]; //[1]
    NSLog(@"[1] : %@",vwTest);

    //or

    UILabel *lblTest = (UILabel *)[self.tableView viewWithTag:100 + indexPath.section]; //[2]
    NSLog(@"%@",lblTest.text);
    UIView *vwTestForSuperview = lblTest.superview;
    NSLog(@"[2] : %@",vwTestForSuperview);
}






PS:我知道这段代码没有任何重要意义,但这只是其他人的一个例子。


PS: I know this code doesn't serve any great purpose but this is just an example for others.

这篇关于UITableView -headerViewForSection返回(null)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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