-1

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){}

Phillip
  • 4,276
  • 7
  • 42
  • 74

1 Answers1

3

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

Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
  • The first method works. The problem i got though is that once it pushes out the View,when i have to go back to the sections, the View Controller goes back to itself twice before getting in the navigation. :S This issue is only on the first 10 elements – Phillip Jul 22 '11 at 17:52
  • 1
    What do you mean by: "the View Controller goes back to itself twice before getting in the navigation"? Have you pushed it twice? I don't think this is related to the use of `indexPath.section` like I showed you... – sergio Jul 22 '11 at 17:56
  • When you push out a controller there is a back button that lets you go back to the nav on the upper-left right? If i press it, it goes back to the same page before going to the Sections navigator. It kinda loops itself twice. I don't think i've pushed it out twice. I'm going to post my code.. – Phillip Jul 22 '11 at 17:59
  • Solved! You save my time! – Genevios Mar 30 '15 at 20:18