What deselect selected cell when navigationController pops a view?

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?


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

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..

链接地址: http://www.djcxy.com/p/60002.html

上一篇: iPhone UITableView单元保持选定状态

下一篇: navigationController弹出视图时取消选定单元格的内容?