In one of my UIViewControllers, I have a UITableView, which only has two rows and both are always visible on screen. This is a table view historically (another developer did it this way – and I’m not stuck with it). Beause both rows of the table are always visible, when my cellForRowAtIndexPath is called, I create one of the cells from a XIB and store a reference to one of the subviews (UITextView). When I need to get hold of the text from that UITextView, I simply use the local reference. This works fine – most of the times.
In one particular case, I navigate to another UIViewController from this one (using pushViewController:animated). That new view controller uses quite a bit of memory, so internally iOS deallocates the table row from the first view controller (it’s not visible any more, because another view controller is active) – and on my return to the first view controller it simply calls cellForRowAtIndexPath again. When that row gets released, naturally the reference I stored internally is no longer valid – and if I try to use it, the app crashes (“selector sent to deallocated instance”).
My question is: how can I catch when iOS internally releases the table cell so that I could set my reference to nil to be re-assigned at a later time?
Apparently, the only way I found to deal with this issue is to include
callback and set the references to
nilthere. Not sure if this is the best way of dealing with the situation, but it works.