Refresh table View data and disarm cell

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

However, since new items are sometimes added, the table is updated by calling reloadDatain viewWillAppear:. This means that none of the cells will be selected when the view appears, even if I have one self.clearsSelectionOnViewWillAppear = NO.

By selecting and deselecting a cell after the appearance of the table view (c viewDidAppear:), the animation time for deselecting is markedly different from the user (try it yourself, it is slower and does not feel slippery).

How can I keep a selection even after updating the table? (Please keep in mind, depending on the situation, I would like to deselect either the previously selected cell or the newly created cell.) Or do I need to somehow reload the data in my table differently?

+3
source share
1 answer

You can save NSIndexPathfrom the method - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath, and when viewing the view again, clear this line.

Another way to do this is by passing the NSIndexPathcurrent one UITableViewControllerto the UIViewControllerone you create, and when this one UIViewControllerpops up, you deselect a specific line.

When a new element is created, add it to the indexPath row element to deselect the correct row.

:

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

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

[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
+6

All Articles