Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 615
Hawk_Pilot Avatar asked Dec 20 '22 19:12

Hawk_Pilot


2 Answers

rename your didDeselectRowAtIndexPath method to didSelectRowAtIndexPath

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

like image 92
Bryan Chen Avatar answered May 22 '23 10:05

Bryan Chen


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.

like image 32
Mohit Avatar answered May 22 '23 08:05

Mohit