prepareforsegue vs didselectrowatindexpath [英] prepareforsegue vs didselectrowatindexpath

查看:63
本文介绍了prepareforsegue vs didselectrowatindexpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在构建一个带有向下钻取功能的表格视图,但遇到了一些问题.我正在使用 prepareforsegue 调用下一个视图,我正在使用 didselectrowatIndexpath 将数据从源视图传递到目标视图.

我有以下问题:1) 当我选择一个单元格时,prepareforsegue 在 didSelectRowAtIndexPath 之前被调用,这意味着在我的数据可以传递到下一个视图之前调用视图2) 我的 didselectrowatIndexpath 中有一个 performeguewithidentifier,它似乎调用了两次 segue(第一次使用 prepareforsegue,第二次使用 performeguewithidentifier).

使用故事板将数据从一个 tableview 传递到另一个 tableview 的最佳做法是什么,调用下一个视图的最佳方法是什么?

这是我的一些代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {如果(级别== 0){int 肌肉ID = [muscleIDArray[indexPath.row] intValue];NSLog(@"肌肉ID: %i", 肌肉ID);NSArray *submuscleArray = [self submuscleGroup:muscleID valueForKeyPath:@"submuscleID"];if (submuscleArray == nil || [submuscleArray count] == 0) {currentMID = 肌肉 ID;等级 = 2;[self performSegueWithIdentifier:@"drillDown2" sender:self];} 别的 {currentMID = 肌肉 ID;等级 = 1;[self performSegueWithIdentifier:@"drillDown" sender:self];}} else if (level == 1) {等级 = 2;currentSMID = [submuscleIDArray[indexPath.row] intValue];[self performSegueWithIdentifier:@"drillDown2" sender:self];} 别的 {detailTitle = [[exerciseList valueForKey:[exerciseListSorted objectAtIndex:indexPath.row]] objectAtIndex:0];运动ID = [[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;运动细节.title = detailTitle;运动细节.exerciseID =运动ID;} else if ([segue.identifier isEqualToString:@"drillDown"] || [segue.identifier isEqualToString:@"drillDown2"]){//UINavigationController *navController = (UINavigationController *)segue.destinationViewController;ExerciseTableViewController *tableView = segue.destinationViewController;tableView.delegate = self;tableView.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 = UIModalTransitionStyleFlipHorizo​​ntal;} else if ([segue.identifier isEqualToString:@"dismissView"]){[自我解雇ViewControllerAnimated:是完成:nil];}}

解决方案

在某些情况下,例如您的示例,您必须根据 indexPath 或其他一些逻辑决定执行哪个 segue,您必须使用 didSelectRowAtIndexPath,并调用performSegueWithIdentifier.但是,当你这样做时,你需要从控制器到下一个视图控制器,而不是从单元格(这可能是你的错误).

如果选择的任何单元格转至同一个控制器,那么最好直接从该单元格进行转场,实施prepareForSegue,而根本不实施didSelectRowAtIndexPath(除非您有其他原因这样做,与传递数据无关到下一个控制器).如果你这样做,你不应该调用 performSegueWithIdentifier.在 prepareForSegue:sender: 中,sender 参数将是单元格,因此您可以使用表视图方法 indexPathForCell: 获取 indexPath,您可以使用它来查询模型,以便将数据传递给目标视图控制器.

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];
    }
}

解决方案

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.

这篇关于prepareforsegue vs didselectrowatindexpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆