簡體   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?

我才剛開始學習Objective-C和程序設計,而我確實對這個問題感到困惑。 我正在使用情節提要創建一個可保留不同類型游戲得分的應用程序。 我有一個主視圖控制器,它是一個表視圖,它具有帶有詳細標簽的原型單元。 有一個AddKeeperViewController ,允許用戶輸入玩家名稱和游戲類型。 然后將游戲類型用作原型單元的詳細標簽。

然后,我想將每個單元格推送到不同的視圖控制器,具體取決於詳細文本標簽是什么。 目前,我只有2種游戲類型,我知道我需要在tableView didSelectRowAtIndexPath中設置邏輯,該邏輯將選擇采用哪種模式。 我是從視圖控制器(而不是單元格)設置的,它們每個都有唯一的標識符。 我只是不知道如何設置我的if語句,以將gameType用作進行選擇的決定因素。

這是我的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];
    }
}

看來您在正確的軌道上。 但是,我認為您等待時間太長,無法選擇要使用的序列。 也許這樣的事情對您有用:

- (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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM