开发者

Preserve Cell Image After Scrolling UITableView

开发者 https://www.devze.com 2023-01-15 19:01 出处:网络
I have a custom UITableViewCell which acts as a check list.As the user clicks on the selected item, it goes from red to green 开发者_StackOverflow社区indicating a selection, however as the users scrol

I have a custom UITableViewCell which acts as a check list. As the user clicks on the selected item, it goes from red to green 开发者_StackOverflow社区indicating a selection, however as the users scrolls further down the list, once they come back up to the top, the image has changed back to it's default red value.

How do I go about preserving the state of an image as the tableview recycles cells?

Thanks


Sounds like you are using the UITableViewCells to store the state of your table data. This is the wrong approach because the cells are reused. You should keep state in an separate 'data store'. This can simply be an array you keep in memory in your UITableViewController subclass or something persistent like SQLite or Core Data. This state is then transferred back to the cell when the table view asks you to in tableView:cellForRowAtIndexPath:.


I had the same problem. Use:

MyTableViewCell* cell = [tableView dequeCellWithReuseIdentifier:@"MyTableViewCell"];
if (cell == nil) { /* create cell */ }
/* and now you reset the properties of that cell */
cell.text = [myCellText objectAtIndex:indexPath.row];

The key here is to reset the properties of dequeued table cell based on the indexPath. This does mean you will have to keep around enough state to re-create any table cell--including the images you are using.

0

精彩评论

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