0
votes

I'm currently building a tableview with drill down and I'm running into some issues. I'm using prepareforsegue to call the next view and I'm using didselectrowatIndexpath to pass data from the source view to the destination view.

I have the following problem: 1) When I select a cell, the prepareforsegue is called before the didSelectRowAtIndexPath which means the view is called before my data can be passed into the next view 2) I have a performseguewithidentifier in my didselectrowatIndexpath and it seems to be calling the segue twice (once initial with the prepareforsegue and the second time with the performseguewithidentifier).

What is the best practice in using storyboard to pass data from one tableview to another and what is the best way to call the next view?

Here are some of my code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (level == 0) {
        int muscleID = [muscleIDArray[indexPath.row] intValue];
        NSLog(@"MuscleID: %i", muscleID);
        NSArray *submuscleArray = [self submuscleGroup:muscleID valueForKeyPath:@"submuscleID"];
        if (submuscleArray == nil || [submuscleArray count] == 0) {

            currentMID = muscleID;
            level = 2;
            [self performSegueWithIdentifier:@"drillDown2" sender:self];
        } else {
            currentMID = muscleID;
            level = 1;
        [self performSegueWithIdentifier:@"drillDown" sender:self];
        }

    } else if (level == 1) {

        level = 2;
        currentSMID = [submuscleIDArray[indexPath.row] intValue];
        [self performSegueWithIdentifier:@"drillDown2" sender:self];

    } else {
        detailTitle = [[exerciseList valueForKey:[exerciseListSorted objectAtIndex:indexPath.row]] objectAtIndex:0];
        exerciseID = [[exerciseListSorted objectAtIndex:indexPath.row] intValue];
        [self performSegueWithIdentifier:@"showExerciseDetail" sender:self];
    }


}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{


    if ([segue.identifier isEqualToString:@"showExerciseDetail"]) {
        //ExerciseDetail *exerciseDetail = (ExerciseDetail *)segue.destinationViewController;
        //exerciseDetail.pageTitle = detailTitle;
        UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
        ExerciseDetail *exerciseDetail = (ExerciseDetail *)navController.topViewController;
        exerciseDetail.title = detailTitle;
        exerciseDetail.exerciseID = exerciseID;
    } else if ([segue.identifier isEqualToString:@"drillDown"] || [segue.identifier isEqualToString:@"drillDown2"]){
        //UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
        ExerciseTableViewController *tableView = segue.destinationViewController;
        tableView.delegate = self;
        tableView.level = level;
        tableView.currentSMID = currentSMID;
        tableView.currentMID = currentMID;
        NSLog(@"level: %i, currentSMID: %i, currentMID: %i", level, currentSMID, currentMID);
    } else if ([segue.identifier isEqualToString:@"addExercise"]){
        UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
        AddExerciseViewController *addExercise = (AddExerciseViewController *)navController.topViewController;
        addExercise.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;    
    } else if ([segue.identifier isEqualToString:@"dismissView"]){
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
2

2 Answers

5
votes

In cases, like your example, where you have to decide which segue to perform based on the indexPath or some other logic, you have to use didSelectRowAtIndexPath, and call performSegueWithIdentifier. However, when you do that, you need to make the segue from the controller to the next view controller, not from the cell (which is probably what your error is).

If selection of any cell segues to the same controller, then it's probably best to make the segue directly from the cell, implement prepareForSegue, and not implement didSelectRowAtIndexPath at all (unless you have another reason to do that, unrelated to passing data to the next controller). If you do it this way, you should not call performSegueWithIdentifier. In prepareForSegue:sender: the sender argument will be the cell, so you can use the table view method indexPathForCell: to get the indexPath, which you can use to query your model for the purpose of passing data to the destination view controller.

0
votes

It think, you have created segue from UITableViewCell from UITableView to nextview, That cause this problem.

You need to setup segue between views (Select firstview, stretch segue from top Yellow circle to next view), by connecting its view controllers.

May this helps you.

Enjoy Coding !!