Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh only the custom header views in a UITableView?

Tags:

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];
}
like image 264
BlueBear Avatar asked May 06 '14 18:05

BlueBear


2 Answers

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];
    }
}
like image 118
Matthias Bauch Avatar answered Oct 26 '22 22:10

Matthias Bauch


Create your own subclass of UIView for the headerView, and add a couple of methods to it so your view controller can send whatever UI updates you want to it.

like image 20
ashack Avatar answered Oct 26 '22 22:10

ashack