Refresh only the custom header views in a UITableView?
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];
}
}
Calling setNeedsDisplay
only serves to trigger the drawRect:
method of the receiving view. Unless this is where your text color logic is (that is, in the drawRect method of your UIView subclass that you are using for header views), nothing is likely to change. What you need to do is directly call something on the header views to update their state based on your new or changed data or directly access the label in the header view and change it yourself while iterating through them. setNeedsDisplay
will only trigger code found in the drawRect:
method of the view.
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.