Search code examples
iphoneiosuitableviewviewwillappear

didDeselectRowAtIndexPath not called after viewWillAppear in UITableViewController


I'm using a UITableViewController subclass with clearsSelectionOnViewWillAppear set to YES (the default value).

How do I make changes to a deselected cell when it is automatically deselected on viewWillAppear:?


Solution

  • tableView:didDeselectRowAtIndexPath: is not called when a cell is deselected automatically on viewWillAppear: in a UITableViewController subclass.

    Use the following code to make changes to the deselected in this case:

    - (void) viewWillAppear:(BOOL)animated {
        // HACK: Need to be called before super if the selection is cleared on viewWillAppear. 
        UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:self.tableView.indexPathForSelectedRow];
        // Change the cell
        [super viewWillAppear:animated];
    }
    

    In general, if you use deselectRowAtIndexPath:animated:, tableView:didDeselectRowAtIndexPath: will not be called.