在分组的UITableViewController中推送视图控制器 [英] Pushing View Controllers in UITableViewController Grouped

查看:123
本文介绍了在分组的UITableViewController中推送视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有3个部分,例如我在上一个问题中所说的,例如"A","B","C".

So, i have 3 section like i said in my previous question, "A", "B", "C", for example.

这是我正在使用的代码:

This is the code i'm using:

- (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的开关,它将推出正确的View Controller.但是当我必须为"B"部分推出包含9个元素的View Controllers时,我继续使用计数器(因此,情况7,8等),它再次从头开始显示控制器(它

This is my problem: in "A" i have 6 elements. If i use the switch from 0 to 5, it pushes out the right View Controllers. But when i have to push out the View Controllers for the "B" section, that contains another 9 elements, i go ahead with the counter (so case 7,8 etc),it starts again to show the controllers from the begin (it pushes out again FirstViewController" etc). Same story for "C" section. How to solve this?

我讨厌分组表格,该死.

I'm hating grouped tables, damn.

新代码循环播放

- (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;



            }
    }
}

我显然已经削减了代码,否则时间太长了.

I've obviously cut the code, too long otherwise.

工作了! Sergio的回答是正确的,但我放了if (indexPath.section == 0)而不是switch (indexPath.section){}

Worked! Sergio's answer was right, but i put an if (indexPath.section == 0) instead of switch (indexPath.section){}

推荐答案

传递了didSelectRowAtIndexPathNSIndexPath indexPath参数具有两个属性:rowsection.如果您正确使用section信息,则可以区分您拥有的三个部分.

The NSIndexPath indexPath argument that didSelectRowAtIndexPath is passed has two properties: row and section. If you properly use the section info, you will be able to distinguish among the three sections you have.

例如(这很丑陋,但可以使用):

E.g. (this is ugly, but it will work):

 - (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类型(另请参见此问题).

A better solution, IMO, would be encoding in your data source the sub table type associated to each element, so that you do not need a big switch and each cell already "knows" what kind of sub table it should open. You can inspect the Class type (look also at this question).

这篇关于在分组的UITableViewController中推送视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆