UITableViewCell doesn't get deselected when swiping back quickly

后端 未结 16 1396
甜味超标
甜味超标 2021-01-29 23:49

I\'ve now updated three of my apps to iOS 7, but in all three, despite them not sharing any code, I have the problem where if the user swipes to go back in the navigation contro

16条回答
  •  走了就别回头了
    2021-01-30 00:52

    This solution animates the row deselection along with the transition coordinator (for a user-driven VC dismiss) and re-applies the selection if the user cancels the transition. Adapted from a solution by Caleb Davenport in Swift. Only tested on iOS 9. Tested as working with both user driven (swipe) transition and the old-style "Back" button tap.

    In the UITableViewController subclass:

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    
        // Workaround. clearsSelectionOnViewWillAppear is unreliable for user-driven (swipe) VC dismiss
        NSIndexPath *indexPath = self.tableView.indexPathForSelectedRow;
        if (indexPath && self.transitionCoordinator) {
            [self.transitionCoordinator animateAlongsideTransition:^(id  _Nonnull context) {
                [self.tableView deselectRowAtIndexPath:indexPath animated:animated];
            } completion:^(id  _Nonnull context) {
                if ([context isCancelled]) {
                    [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
                }
            }];
        }
    }
    

提交回复
热议问题