简体   繁体   English

如何根据表视图单元格的详细文本标签,将segue设置为与表视图控制器不同的视图控制器?

[英]How can I set a segue to a different view controller from a table view controller, based on the detail text label of the table view cell?

I am just beginning to learn objective-c and programming in general, and I am really stuck on this issue. 我才刚开始学习Objective-C和程序设计,而我确实对这个问题感到困惑。 I am using storyboards to create an app that keeps scores for different types of games. 我正在使用情节提要创建一个可保留不同类型游戏得分的应用程序。 I have a master view controller that is a table view and it has prototype cells with detail labels. 我有一个主视图控制器,它是一个表视图,它具有带有详细标签的原型单元。 There is an AddKeeperViewController that allows the user to input player names and the game type. 有一个AddKeeperViewController ,允许用户输入玩家名称和游戏类型。 The game type is then used as the detail label for the prototype cells. 然后将游戏类型用作原型单元的详细标签。

I want to then have each cell push to a different view controller, depending on what the detail text label is. 然后,我想将每个单元格推送到不同的视图控制器,具体取决于详细文本标签是什么。 I only have 2 game types at the moment, I know I need to set up logic in tableView didSelectRowAtIndexPath that will choose which segue to take. 目前,我只有2种游戏类型,我知道我需要在tableView didSelectRowAtIndexPath中设置逻辑,该逻辑将选择采用哪种模式。 I set up my segues from the view controller, not the cells, and they each have a unique identifier. 我是从视图控制器(而不是单元格)设置的,它们每个都有唯一的标识符。 I just don't know how to set up my if statements to use the gameType as the deciding factor for which segue to take. 我只是不知道如何设置我的if语句,以将gameType用作进行选择的决定因素。

Here is some of my code from my MasterViewController.m file: 这是我的MasterViewController.m文件中的一些代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"scoreKeeperCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    scoreKeeper *keeperAtIndex = [self.dataController objectInListAtIndex:indexPath.row];

    [[cell textLabel]setText:keeperAtIndex.name];
    [[cell detailTextLabel] setText:keeperAtIndex.gameType];

    return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if(cell.detailTextLabel) {
          //statement
    }
    if ([[segue identifier] isEqualToString:@"ShowKeeperDetails"]) {
        MADDetailViewController *detailViewController = [segue destinationViewController];

        detailViewController.keeper = [self.dataController objectInListAtIndex:[self.tableView indexPathForSelectedRow].row];
    }
}

It seems like you're on the right track. 看来您在正确的轨道上。 However, I think you're waiting a bit too long to choose which segue to take. 但是,我认为您等待时间太长,无法选择要使用的序列。 Perhaps something like this would work for you: 也许这样的事情对您有用:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if ([cell.detailTextLabel.text isEqualToString:@"TEXT"]) 
    {
        [self performSegueWithIdentifier:@"segueTypeOne" sender:nil];
    } 
    else 
    {
        [self performSegueWithIdentifier:@"segueTypeTwo" sender:nil];
    }
}

您还可以创建不同单元的原型,每个原型都与导致不同视图的不同segue连接,然后使用正确的原型在dequeueReusableCellWithIdentifier获取单元。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用表格视图单元格中的文本并将其显示为父视图控制器中的标签? - How can I use text from a table view cell and display it as a label in a parent view controller? 从表视图控制器Segue到主从控制器 - Segue to Master Detail Controller from Table View Controller 从替换详细信息控制器的表视图单元格中搜索不会触发 - segue from table view cells that replace detail controller do not fire 从不同的表视图单元格“ segue”到同一视图控制器 - 'segue' to same view controller from different table view cells 选项卡和导航视图控制器中的Table单元格的Segue - Segue of Table cell in a tab and navtigation view controller segue表视图以查看控制器和表视图 - segue table view to view controller and table view 根据在其他Table View Controller中单击的内容,选择到Table View Controller - Segue to a Table View Controller based on what was clicked on in a different Table View Controller 如何通过基于选项卡的视图控制器快速管理5种不同的表视图(和详细视图)? - How to manage 5 different table views (and detail views) from a tab-based view controller in swift? 如何从表视图控制器转到详细信息控制器然后返回 - How to go from table view controller to a detail controller and go back 如何将信息从一个视图控制器中的表视图单元传递到另一视图控制器? - How do I pass information from a table view cell in one view controller to another view controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM