UITableView with custom cell subview - respondsToSelector:]: message sent to deallocated instance -


i have viewcontroller contains uitableview in cells created via custom subview (so each cell have label , textfield). subview includes textfield delegate methods fired on textfielddidbeginediting, textfielddidendediting, etc. uitableview caches cells visible , destroys them when cell no longer visible. problem have when user taps on textfield in cell , scrolls table cell being "edited" out of view , taps on new cell edit it, original cell's textfielddidendediting delegate method called, causing "respondstoselector:]: message sent deallocated instance" error because original/first textfield has been destroyed uitableview when scrolled out of visible area.

has dealt issue before , found solution can keep textfielddidendediting methods proper handing of data user's input without having now-invisible cells destroyed?

edit: found in uitableview class reference doc: "avoid relayout of content. when reusing cells custom subviews, refrain laying out subviews each time table view requests cell. lay out subviews once, when cell created."

this is, think, root of issue. not sure how follow advice. help...

"the uitableview caches cells visible , destroys them when cell no longer visible." that's true if don't provide reuse identifier.

assuming you're working on detail view each cell presenting (conceptually @ least) property of model object, can assign each cell unique reuse identifier. can set directly in interface builder, or, if you're creating cells programmatically, passing argument initwithstyle:reuseidentifier: or initwithframe:reuseidentifier.

given reuse identifier, table view cache cells entire lifetime. cell cache use

cell = [tableview dequeuereusablecellwithidentifier:@"some identifier made up"]; 

again, use different identifiers distinguish unique cells, if necessary.

an alternative detail view has fixed number of unique cells store reference each cell in instance variable (or store whole group of them in collection). long retain instance variables, cells won't deallocated if didn't bother providing reuse identifiers.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -