我有一个带有按钮主页的 Storyboard,触摸按钮会转到带有嵌入式表格 View Controller 的导航 Controller 。当表格 View 出现时,它会填充一个项目列表。当用户触摸列表中的某个项目时,我想推送另一个表格 View 或详细信息 View 。
这就是我正在做的事情:
在我的 ListTableViewController 中,我这样做是我的 didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int row = [indexPath row];
DSObject * obj = _m_DSObjects[row];
if (obj.nType == 1)
{
ListTableViewController * tvc = [[ListTableViewController alloc]
initWithStyle:UITableViewStylePlain];
[self.navigationController pushViewController:tvc animated:YES];
}
else
{
MyDetaisViewController * vc = [[MyDetaisViewController alloc]
initWithStyle:UITableViewStylePlain];
[self.navigationController pushViewController:vc animated:YES];
}
}
基本上,如果它是类型 1(另一个列表),那么我想创建另一个 ListViewController,但如果它是类型 2(详细信息),那么我想显示我的详细信息 View 。
我的问题是,如果它是一个列表,我的列表不会填充项目。单元格行在那里,但是是空白的,它似乎没有使用我的单元格原型(prototype)。
如果我做错了,我会感到困惑,并正在寻找如何完成我需要的建议。
感谢您的帮助!!
最佳答案
由于您已经在使用 Storyboard ,更好的方法是创建从 ListTableViewController
到新列表 Controller 以及详细信息 View Controller 的 Segue。
然后您可以将 didSelectRowAtIndexPath
更改为如下所示:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int row = [indexPath row];
DSObject * obj = _m_DSObjects[row];
if (obj.nType == 1)
{
// assuming you named your segue "PresentListController"
[self performSegueWithIdentifier:@"PresentListController" sender:self];
}
else
{ // assuming you named your segue "PresentDetailsController"
[self performSegueWithIdentifier:@"PresentDetailsController" sender:self];
}
}
如果您需要将参数传递给其中任何一个,您可以使用prepareForSegue
:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.destinationViewController isKindOfClass:[ListTableViewController class]]) {
ListTableViewController* listController = (ListTableViewController*)segue.destinationViewController;
listController.someParam = someValue;
return;
}
if ([segue.destinationViewController isKindOfClass:[MyDetailsViewController class]]) {
MyDetailsViewController* detailsController = (MyDetailsViewController*)segue.destinationViewController;
detailsController.someOtherParam = someOtherValue;
return;
}
}
编辑:
在 prepareForSegue
中,您可以使用 segue.identifier
检查,而不是检查 destinationViewController
的 class
正确的转场,只是检查转场去向的另一种方法。
关于iphone - 关于 UITableViewController Storyboard的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12975927/