表格视图选择新视图
这是我目前拥有的代码:
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查indexPath.row ==是否是你想要的行。如果您的表格是静态的,则只能在故事板中执行此操作。
Check if indexPath.row == to the row you want. You could only do it in storyboard if your table was static.