Refresh only the custom header views in a UITableView?
ios, objective-c, uitableview
Solution
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];
}
}
Problem
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]; } ```