Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

ios - Refresh only the custom header views in a UITableView?

My UITableView has custom UIView headers in every section. I am needing to refresh only the headers and not the other content in the section. I have tried out [self.tableView headerViewForSection:i] and it does not return anything even though it should. Is there anyway that I can do this?

Edit: Code based around new suggestion

I have given this a shot as well and it calls/updates the UIView within that method, but the changes do not visually propagate onto the screen.

for (int i = 0; i < self.objects.count; i++) {
    UIView *headerView = [self tableView:self.tableView viewForHeaderInSection:i];
    [headerView setNeedsDisplay];
}
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Instead of calling setNeedsDisplay, configure the header yourself by setting it's properties. And of course you have to get the actual headers in the table, don't call the delegate method, because that method usually creates a new header view.

I usually do this in a little helper method that is called from tableView:viewForHeaderInSection: as well.

e.g.:

- (void)configureHeader:(UITableViewHeaderFooterView *)header forSection:(NSInteger)section {
    // configure your header
    header.textLabel.text = ...
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UITableViewHeaderFooterView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"Header"];
    [self configureHeader:header forSection:section];
}

- (void)reloadHeaders {
    for (NSInteger i = 0; i < [self numberOfSectionsInTableView:self.tableView]; i++) {
        UITableViewHeaderFooterView *header = [self.tableView headerViewForSection:i];
        [self configureHeader:header forSection:i];
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...