Pushing View Controllers in UITableViewController Grouped

China☆狼群 提交于 2019-12-26 03:55:11

问题


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

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.

Edit: new code, looping

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

Edit 2:

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


回答1:


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

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).



来源:https://stackoverflow.com/questions/6793847/pushing-view-controllers-in-uitableviewcontroller-grouped

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!