开发者

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 jus开发者_JS百科t 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..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜