开发者

Is it possible to preload all the cells in a uitableview?

开发者 https://www.devze.com 2022-12-10 14:48 出处:网络
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开

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!


Don't use the cells as your database.

Your cells are just a narrow window onto your data. The cells just show you a few of the objects in the underlying data. If you try to preload all the cells so you could then select them all, the UITableView could die a slow death, or slow crawl. Especially if we're talking hundreds of entries.

If you want to select all the items in your data, you do so with a direct call to your data to select its objects. Then, you reload the data into your TableView with a reloadData and if everything is set up right, your cells will show the selected state.

Read up on UITableView. Look at Apple's samples. You need to see the separation of data from the view and the controller.


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 NSMutableArray or a C array of int or BOOL values, 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 the ith row of the table view. If you use NSMutableArray to store NSNumbers, 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 NSMutableArray to store NSMutableArrays of NSNumbers, 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:

  1. Loop through the array and set each value to YES or 1 or whatever "on" label you chose originally.

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


No, you can't do this. You seem to be under the impression that a cell represents a particular piece of data, it doesn't. When the top cell scrolls off the screen it is usually recycled and brought in as the bottom cell. So a list that has hundreds of items you can scolled through may only ever have 8 or 9 cells alloc'ed and initialized.

You need to rethink your application's architecture. When you "uncheck all" it shouldn't change the visual state of the cell, it should change some state in the objects the cell represents, then when you load the cell for the object at that index path you should read that state and set the check mark appropriately.

The changes in the visual state of your cell should always be in response to changes in your underlying model.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号