views:

1256

answers:

4

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!

+1  A: 

There 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.

Sixten Otto
It seems like I might have to use reloadSections, even though it's nearly as slow as reloading the whole table (for my app at least). I have been foiled at every turn by the Cocoa API. I tried using a circular queue to reuse a set of UILabel's and returning them in viewForHeader, but viewForHeader is called for so many cells (even ones that aren't visible!), that I needed to create a separate UILabel for every cell, which is just plain bad. Argh!
mathew
A: 

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?
}
Clay Bridges
A: 

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.

David Liu
A: 

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:
Scott McCammon