所以,我有 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 的开关,它会推出正确的视图控制器。但是,当我必须推出包含另外 9 个元素的“B”部分的视图控制器时,我继续使用计数器(例如 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){}