views:

87

answers:

2

I have a multi-section UITableView with custom header views that need to know their section index. I currently record the section number in the tag field of UIView when creating the custom view in viewForHeaderInSection. However, when a row is deleted, the UITableView does not reload the section header views for header views visible on the screen, so their tag fields become out of sync. Calling reloadData on the table re-syncs the tag fields by recreating the header views, but calling reloadData interferes with the row deletion animation. Setting up a timer to call reloadData after a "short" period of time seems hacky and somewhat risky if the user ends up interacting with an out-of-sync header before the timer fires (I suppose I could add a state variable to prevent this, ugh).

I could I suppose keep track of all header views created in a container, and adjust their tags when rows are deleted (ugh again). But then how do I avoid leaking the Views? How do I know when the UITableView has released the view so I can delete my reference to them so they can be released?

This seems like way more work than it should be....am I missing something? I've noticed others have very similar issues and never seen a definitive approach to solving it.

A: 

You shouldn't need to reload the table; just update the tag property. In your code where you delete the rows; do you have access to the header views? If not, you can use the observing pattern to alert the header views when a row has been deleted.

GendoIkari
I know I could keep track of the header views (as I noted in my question), but how do I avoid leaking them?
Bogatyr
A: 

I have a multi-section UITableView with custom header views that need to know their section index. I currently record the section number in the tag field of UIView when creating the custom view in viewForHeaderInSection.

How about a dictionary owned by the controller that records each section number for each view. Since you have the recorded section number dependent on the tableview you have to wait for it to refresh (which you shouldn't have to call often yourself.)

You just have to find/make a way to have keys for each view so you know which one is which regardless of the order.

Setting up a timer to call reloadData after a "short" period of time seems hacky and somewhat risky if the user ends up interacting with an out-of-sync header before the timer fires (I suppose I could add a state variable to prevent this, ugh).

I agree that it seems hacky, good instinct. Adding a state variable to prevent this would be less work than a dictionary and tracking system for each view, but I think it would also fall under 'hacky solutions'.

JoePasq
Right, I could keep track of the header views, as I noted in my question, but I asked: how do I avoid leaking them? When is a table view "done" with a custom header/footer view? If I retain a reference to the header views (e.g., in a dictionary) so I can update their tags, how do I know when to release them? p.s. using the delayed reload with state variable to prevent the out-of-sync use of a view was really simple and worked to preserve the section deletion animation. But I still would like to know how to do this "right," and to know when I can release the views from my dictionary.
Bogatyr
When all the rows in the section are gone then the header and footer should go away too. The thing is you have to be smart with multiple sections about which is which, being careful and using some sort of check that you have the right view at the right index to remove.
JoePasq
At some point when a section scrolls off the screen the UITableView will release the custom view for that section. I'd like to know when that happens. Is that possible? Otherwise, I have to hold on to all of my custom header views forever until the table is destroyed. That's what I was trying to avoid.
Bogatyr
When viewDidDisappear is called.
JoePasq
"Notifies the view controller that its view was dismissed, covered, or otherwise hidden from view.- (void)viewDidDisappear:(BOOL)animated"For each section header view, individually.
JoePasq