Reload Table View Data and Deselect Cell

I have a table view where I'd like to deselect either the previously selected cell when returning to it from a detail view or the newly added cell when the user creates an item.

However, since sometimes new items are added, the table is refreshed by calling reloadData in viewWillAppear: . This means none of the cells are selected when the view appears, even if I have self.clearsSelectionOnViewWillAppear = NO .

By selecting and deselecting the cell after the table view appears (in viewDidAppear: ) the timing of the deselect animation is visibly different to the user (try for yourself, it's slower and doesn't feel as slick).

How should I be preserving the selection even after the table view is refreshed? (Please keep in mind, depending on the situation, I'd like the deselect either the previously selected cell or the newly created cell.) Or should somehow I be reloading the data in my table differently?


You could save the NSIndexPath from the - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method and when the view reloads deselect that row.

Another way of doing this could be by passing the NSIndexPath and the current UITableViewController to the UIViewController you're creating and when that UIViewController is popped, you deselect the specific row.

When a new item is created, add one to the indexPath's row element to deselect the right row.

You could also reload only the rows that have changed:

[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                      withRowAnimation:UITableViewRowAnimationNone];

[self.tableView selectRowAtIndexPath:indexPath 
                            animated:NO
                      scrollPosition:UITableViewScrollPositionNone];

[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
链接地址: http://www.djcxy.com/p/60014.html

上一篇: UITableView在编辑后取消选择所选单元格

下一篇: 重新载入表格视图数据并取消选择单元格