What deselect selected cell when navigationControl

2019-07-28 07:06发布

问题:

The default NavigationController template offered by apple, it has one navigationController and a table.

And if you select a cell, a new view will be pushed into navigationController and if you pop the view, the selected cell will be de-hightlighted automatically.

But how does table know when to de-hightlight it and how does it know which cell is selected??

or does it just re-load all data again?

回答1:

how does table know when to de-hightlight it

You can deselect your cell right in selection handler:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath: indexPath];
    ...
}

or reset selection in controller's -viewWillAppear: method

and how does it know which cell is selected?

UITableView has the following method to get selected row indexPath:

- (NSIndexPath *)indexPathForSelectedRow


回答2:

For Swift 3.0

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    if self.yourTableView.indexPathForSelectedRow != nil
    {
        self.yourTableView.deselectRow(at: self.yourTableView.indexPathForSelectedRow!, animated: true)
    }
}

This code will avoid the Crash as well...

Also, add the below line in the other ViewController whom you're pushing while selecting a TableViewCell.

self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true

Works like a Charm =]

Hope it helps..