Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

iphone - uitableview headerViewForSection: always returns nil

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];
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...