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];
}
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];
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With