【问题标题】:Pushing View Controllers in UITableViewController Grouped在 UITableViewController 中推送视图控制器分组
【发布时间】:2011-07-22 17:34:06
【问题描述】:

所以,就像我在上一个问题中所说的那样,我有 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){}

【问题讨论】:

  • 这看起来你应该使用indexPath.section,而你有indexPath.row

标签: objective-c ios xcode4 grouped-table


【解决方案1】:

didSelectRowAtIndexPath 传递的 NSIndexPath indexPath 参数有两个属性:rowsection。如果您正确使用section 信息,您将能够区分您拥有的三个部分。

例如(这很丑陋,但它会起作用):

 - (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...
   }
}

IMO 更好的解决方案是在您的数据源中编码与每个元素关联的子表类型,这样您就不需要大开关,并且每个单元格已经“知道”它应该打开什么样的子表。您可以检查Class 类型(另请查看this question)。

【讨论】:

  • 第一种方法有效。我遇到的问题是,一旦它推出视图,当我必须返回部分时,视图控制器在进入导航之前会返回自身两次。 :S 这个问题只针对前 10 个元素
  • 你是什么意思:“视图控制器在进入导航之前会回到自己两次”?你推了两次?我不认为这与 indexPath.section 的使用有关,就像我向你展示的那样......
  • 当你推出控制器时,有一个返回按钮可以让你回到左上角的导航?如果我按下它,它会在转到“部分”导航器之前返回到同一页面。它有点自循环两次。我不认为我已经推出了两次。我要发布我的代码..
  • 解决了!感谢您的回答!
  • 解决了!你节省了我的时间!
猜你喜欢
  • 1970-01-01
  • 2014-12-07
  • 2016-10-22
  • 2010-12-11
  • 1970-01-01
  • 1970-01-01
  • 2014-01-11
相关资源
最近更新 更多