I'm trying to change the header title for a section in a UITableView when a cell from that section is selected. tableView:titleForHeaderInSection is triggered by the application, so that doesn't help. I can call reloadData, but the performance suffers because the app has to reload all the visible cells. I also tried to use a custom header, but that also results in some performance problems. Is there any way to get a handle to the UILabel that the default header view uses and change its text manually? Thanks!
views:
1256answers:
4There doesn't appear to be any standard API for accessing the system-provided section header view. Have you tried the more targeted reloadSections:withRowAnimation
to get UIKit to display the new header text?
What kind of performance issues were you seeing with custom section header views? I doubt that the standard one is much more than just a UILabel
.
This is a WAG, and I can think of lots of reasons why it might not work, but couldn't you iterate through the subviews, and find the one you are looking for? E.g.
for (UIView *v in self.tableView.subviews) {
// ... is this the one?
}
Since a UITableView does not enqueue and dequeue section header views for reuse, you might as well see if it's feasible to store all the section header views in memory. Note you'll have to create your own section header views with the background and etc., to do this, but it allows you a bit more flexibility and capability.
You could also try tagging the section header views (also requires you to create your own section header views) and just grab them from the tableview as needed.
Calling [tableView endUpdates]
may provide the desired results without too much of a performance hit.
[self.tableView beginUpdates];
[self.tableView endUpdates];
// forces the tableView to ask its delegate/datasource the following:
// numberOfSectionsInTableView:
// tableView:titleForHeaderInSection:
// tableView:titleForFooterInSection:
// tableView:viewForHeaderInSection:
// tableView:viewForFooterInSection:
// tableView:heightForHeaderInSection:
// tableView:heightForFooterInSection:
// tableView:numberOfRowsInSection: