
[英]How can I use text from a table view cell and display it as a label in a parent view controller?
[英]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.