Is there a simple way to preload all the cells in a uitableview?
I need to do this in order to change whether or not all the cells are checked. If I just use cellForRowAtIndexPath, and the user say unchecks all the cells, and then checks a visible cell and starts to scroll again, either the selected cell gets deselected or the newly loading cells are selected.
It seems the easiest way to go would be to preload all the cells if possible.
Thanks!
Please re-read the answer I wrote here to your previous, similar question, which explains one solution to your problem.
Again, you should consider keeping an array of on/off settings. You can use
NSMutableArrayor a C array ofintorBOOLvalues, whatever you want. But it definitely sounds like you need a data model.Your
-tableView:cellForRowAtIndexPath:looks up values of this array. Each value in the array corresponds in some way to a row in the table.If you only have one section, then you can simply use the
ith element of the array to set the checked state of theith row of the table view. If you useNSMutableArrayto storeNSNumbers, you can handle resizing quite easily.If you have more than one section, keep an array of arrays. Each top-level array corresponds to a section. Each inner array corresponds to a section’s rows.
If you use
NSMutableArrayto storeNSMutableArrays ofNSNumbers, you can handle resizing and section addition and deletion quite easily.The method
-tableView:cellForRowAtIndexPath:then sets up cells with or without checkmarks, depending on the array’s value.Having a data model gives you the freedom to programmatically perform “select all” and “deselect all” button operations.
For example, when you click a button to “select all” cells:
Loop through the array and set each value to
YESor1or whatever “on” label you chose originally.Call
[tableView reloadData], which is a method that goes back to-tableView:cellForRowAtIndexPath:and which will set a cell’s checkmark state based on the state of values in the updated array.