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
NSIndexPathfrom the- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPathmethod and when the view reloads deselect that row.Another way of doing this could be by passing the
NSIndexPathand the currentUITableViewControllerto theUIViewControlleryou’re creating and when thatUIViewControlleris 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: