Why can't I call reloadData AND deselectRowAtIndexPath in viewWillAppear:animated at the same time?

前端 未结 2 1458
夕颜
夕颜 2021-02-11 03:53

I have a UITableView with cells that push viewControllers onto the stack when selected. The child viewControllers take user input and then pops off the stack.

When the c

2条回答
  •  一整个雨季
    2021-02-11 03:57

    When you reload a cell, it automatically gets deselected. That's because you don't set a cell's selected property to YES in tableView:cellForRowAtIndexPath:. So you will have to deal with this differently. Either identify that the cell at indexPath needs to be selected and appropriately set its selected property to YES in tableView:cellForRowAtIndexPath: or select it after you reload the data. In such case, you can execute the following methods –

    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                          withRowAnimation:UITableViewRowAnimationNone];
    
    [self.tableView selectRowAtIndexPath:indexPath 
                                animated:NO
                          scrollPosition:UITableViewScrollPositionNone];
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    

    The order of steps are :-

    1. Reload the row without any animation.
    2. Select the row without animation.
    3. Deselect the row with animation.

    This way I think you can get the effect you want.

提交回复
热议问题