所以,我有3个部分,就像我在上一个问题中说的那样,例如“A”,“B”,“C”。
这是我正在使用的代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.row)
{
case 0:
[self.navigationController pushViewController:[[[FirstViewController alloc] initWithNibName:nil bundle:nil] autorelease] animated:YES];
break;
case 1:
[self.navigationController pushViewController:[[[SecondViewController alloc] initWithNibName:nil bundle:nil] autorelease] animated:YES];
break;
etc..
}
}
这是我的问题:在“A”中我有6个元素。如果我使用从0到5的开关,它会推出右视图控制器。但是,当我必须推出视图控制器的“B”部分,其中包含另外9个元素时,我继续使用计数器(所以情况7,8等),它再次开始从开始显示控制器(它推出FirstViewController“等等。”C“部分的故事。如何解决这个问题?
我讨厌分组表,该死的。
编辑:新代码,循环
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section)
{
case 0:
switch (indexPath.row) {
case 0:
[self.navigationController pushViewController:[[[FirstViewController alloc] initWithNibName:nil bundle:nil] autorelease] animated:YES];
break;
case 1:
[self.navigationController pushViewController:[[[SecondViewController alloc] initWithNibName:nil bundle:nil] autorelease] animated:YES];
break;
}
case 1:
switch (indexPath.row) {
case 0:
[self.navigationController pushViewController:[[[ThirdViewController alloc] initWithNibName:nil bundle:nil] autorelease] animated:YES];
break;
case 1:
[self.navigationController pushViewController:[[[FourthViewController alloc] initWithNibName:nil bundle:nil] autorelease] animated:YES];
break;
case 2:
[self.navigationController pushViewController:[[[FifthViewController alloc] initWithNibName:nil bundle:nil] autorelease] animated:YES];
break;
}
case 2:
switch (indexPath.row) {
case 0:
[self.navigationController pushViewController:[[[SixthViewController alloc] initWithNibName:nil bundle:nil] autorelease] animated:YES];
break;
case 1:
[self.navigationController pushViewController:[[[SeventhViewController alloc] initWithNibName:nil bundle:nil] autorelease] animated:YES];
break;
}
}
}
我显然已经削减了代码,否则就太长了。
编辑2:
工作!塞尔吉奥的答案是对的,但我提出if (indexPath.section == 0)
代替switch (indexPath.section){}
答案 0 :(得分:3)
传递NSIndexPath
的{{1}} indexPath
参数有两个属性:didSelectRowAtIndexPath
和row
。如果您正确使用section
信息,您将能够区分您拥有的三个部分。
E.g。 (这很难看,但它会起作用):
section
更好的解决方案IMO将在您的数据源中编码与每个元素关联的子表类型,这样您就不需要大的开关,并且每个单元已经“知道”应该打开哪种子表。您可以检查 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section)
{
case 0:
switch (indexPath.row)
{
case 0:
...
break;
...
}
break;
case 1:
switch (indexPath.row)
{
case 0:
...
break;
...
}
break;
etc...
}
}
类型(另请参阅this question)。