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
406 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 Answer

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

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

...