uitableview headerViewForSection:始终返回nil [英] uitableview headerViewForSection: always returns nil

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

问题描述

我有一个包含自定义标题视图的表,无论何时,或者我为部分选择了什么值,我总是得到零值。我有另一个表有同样问题的表。



如果我打印[tableview subviews]的值,我可以看到标题视图,但我不知道为什么这个方法不会返回任何内容。



我要做的是获取headerview中的activityIndi​​cator并启动它或使用方法调用停止它。



标题总是画好,但我无法获得参考。此外,调用 headerViewForSection:不会调用委托方法,这是正常的吗?



footerViewForSection:有同样的问题



部分代码:

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

NSArray * objs = [[NSBundle mainBundle] loadNibNamed:@iPadTableCells所有者:零选项:零];
UIView * header = [objs objectAtIndex:0];

UIActivityIndi​​catorView * activityIndi​​cator =(UIActivityIndi​​catorView *)[header viewWithTag:5];
[activityIndi​​cator startAnimating]

返回标题;

}

从任何方法:

  UIView * headerView = [tableview headerViewForSection:section]; //返回nil 

if(headerView){
UIActivityIndi​​catorView * activityIndi​​cator =(UIActivityIndi​​catorView *)[headerView viewWithTag:5];
[activityIndi​​cator stopAnimating];
}


解决方案

回答



来自文档:


要使表格视图能够识别页眉或页脚视图,需要注册。你可以使用 registerNib:forHeaderFooterViewReuseIdentifier: registerClass:forHeaderFooterViewReuseIdentifier: UITableView的方法


(Swift等价物是寄存器(_:forHeaderFooterViewReuseIdentifier:)。)



所以你需要注册nib,然后使用重用标识符来获取它,而不是直接从应用程序包中取出它,这就是你现在正在做的事情。



...如果你想使用 headerViewForSection 方法。 / p>

替代答案



或者,您可以检查是否继续在 viewForHeaderInSection 方法然后发送只是调用:

  [self.tableView beginUpdates]; 
[self.tableView endUpdates];

刷新章节标题。



(请注意,这种替代方法会破坏并重新创建整个视图,因此如果您有一个包含大量数据的大表,则可能效率不高。)


I have a table with custom header views that no matter when, or what value I choose for section, I always get nil value. I have another table with the same problem.

I can see the header views if I print the value of [tableview subviews], but I don't know why the method won't return anything.

What I am trying to do is to get an activityIndicator that is in the headerview and start it or stop it with a method call.

The headers are always painted ok, but I can't get a reference back to it. Also, calling headerViewForSection: doesn't call the delegate method, is that normal?

footerViewForSection: has the same problem

Some code:

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

    NSArray* objs = [[NSBundle mainBundle] loadNibNamed:@"iPadTableCells" owner:nil options:nil];
    UIView* header = [objs objectAtIndex: 0];

    UIActivityIndicatorView* activityIndicator = (UIActivityIndicatorView*) [header viewWithTag:5];
    [activityIndicator startAnimating]

    return header;

}

from any method:

    UIView* headerView = [tableview headerViewForSection: section];  //returns nil

    if (headerView) {
        UIActivityIndicatorView* activityIndicator = (UIActivityIndicatorView*)[headerView viewWithTag: 5];
        [activityIndicator stopAnimating];
    }

解决方案

Answer

From the docs:

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

(The Swift equivalent is register(_:forHeaderFooterViewReuseIdentifier:).)

So you need to register the nib, and then get it using a reuse identifier, instead of pulling it straight out of the app bundle, which is what you're doing now.

...if you want to use the headerViewForSection method.

Alternate Answer

Alternatively, you could check whether to keep spinning inside the viewForHeaderInSection method and then send just call:

[self.tableView beginUpdates];
[self.tableView endUpdates];

To refresh the section header.

(Note that this alternative approach will destroy and recreate your whole view, so if you have a big table with lots of data, it may not be very efficient.)

这篇关于uitableview headerViewForSection:始终返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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