开发者

Pushing View Controllers in UITableViewController Grouped

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 star开发者_开发技巧ts 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){}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜