表格视图选择新视图

发布于 2024-12-25 20:41:00 字数 1193 浏览 3 评论 0原文

这是我目前拥有的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *__strong)indexPath {
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"Detail"];
[self.navigationController pushViewController:detail animated:YES];

detail.rowNum.text = [NSString stringWithFormat:@"Image %d", (indexPath.row + 1)];

}

它为表视图上的每个单元格加载相同的视图控制器。我如何声明我只希望在选择某一特定行时发生这种情况?因此,例如我会让上述内容仅适用于第一行,每个额外行都有另一个实例?或者,这可以在 Xcode 的 Storyboard 部分完成吗?

更新:根据答案,我已将代码更新为工作正常,所以我想使用 if 没有 else 就可以了?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *__strong)indexPath {
if (indexPath.row == 0) {
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"Detail"];
[self.navigationController pushViewController:detail animated:YES];

detail.rowNum.text = [NSString stringWithFormat:@"Image %d", (indexPath.row + 1)];
}

if (indexPath.row == 1) {
    DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"Forum"];
    [self.navigationController pushViewController:detail animated:YES];

}

}

Here is the code I have at present:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *__strong)indexPath {
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"Detail"];
[self.navigationController pushViewController:detail animated:YES];

detail.rowNum.text = [NSString stringWithFormat:@"Image %d", (indexPath.row + 1)];

}

which loads the same view controller for every cell on my table view. how do i state that i only want this to happen when one particular row is selected? so for instance i would make the above apply only to row one with another instance for each extra row? alternatively, can this much be done in the storyboard section of Xcode?

UPDATE: following an answer I've updated the code to this which works fine, so i guess it's fine to use if without else?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *__strong)indexPath {
if (indexPath.row == 0) {
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"Detail"];
[self.navigationController pushViewController:detail animated:YES];

detail.rowNum.text = [NSString stringWithFormat:@"Image %d", (indexPath.row + 1)];
}

if (indexPath.row == 1) {
    DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"Forum"];
    [self.navigationController pushViewController:detail animated:YES];

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追我者格杀勿论 2025-01-01 20:41:00

检查indexPath.row ==是否是你想要的行。如果您的表格是静态的,则只能在故事板中执行此操作。

Check if indexPath.row == to the row you want. You could only do it in storyboard if your table was static.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文