why does my tableview not call didselectrowatindexpath until 2nd touch?

763 views Asked by At

I have a parent table view with custom rows, each one pushes a view controller with a tableview of selectable options that are stored as properties of the parent view controller. My code:

- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
  /*
    CrewPositions *selectedPosition = (self.crewPositionList)[indexPath.row];
    [self.delegate childCrewPositionDidSelect:selectedPosition];
    NSLog(@"child Selected Position: %@", selectedPosition.title);
    [[self navigationController] popViewControllerAnimated:YES];
  */
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    CrewPositions *selectedPosition = (self.crewPositionList)[indexPath.row];
    self.selectedCrewPosition = selectedPosition;
    NSLog(@"self.selectedCrewPosition: %@", self.selectedCrewPosition);
    selectedFlightHistory.crewPosition = selectedPosition;
    NSLog(@"self.selectedFlightHistory.crewPosition: %@", selectedFlightHistory.crewPosition.title);

    [self.delegate childCrewPositionDidSelect:selectedPosition];

    NSLog(@"Popping view to parent");
    [self.navigationController popViewControllerAnimated:YES];
}

works properly, unfortunately the method didSelectRowAtIndexPath does not get called until after I tap the list of cells a 2nd time. When I do, it passes the first row, previously selected, to the parent view. Ignore the copious amounts of NSLog, just capturing what the variables are doing.

2

There are 2 answers

3
Bryan Chen On

rename your didDeselectRowAtIndexPath method to didSelectRowAtIndexPath

it is called on 2nd time because your select another cell and deselect the previous one

1
Mohit On

It's happening because you wrote wrong method name. didDeselectRowAtIndexpath: is sent when the row is de -selected.

So change the method name didSelectRowAtIndexPath: So that the implementation is for the correct delegate method.