Search code examples
uitableviewuinavigationcontroller

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?


Solution

  • 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