views:

55

answers:

1

I am having trouble setting up my cellForRowAtIndexPath with Sections. If I were to just show all "tracks" without separations of sections by "topics" then it works fine.

Track *aTrack = [appDelegate.tracks objectAtIndex:row];
cell.textLabel.text = aTrack.track_title;

I have already set up my numberOfSectionsInTableView, numberOfRowsInSection, titleForHeaderInSection. Those all works fine. But I don't really know how to setup cellForRowAtIndexPath.

After researching some more the best way seems to use a NSDictionary to display my cells...

NSDictionary *dictionary = [sectionTopicArray objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"Tracks"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

Can someone give me some pointers on how to make a MutableArray(sectionTopicArray) that is gonna hold all my Dictionaries? I can see the following in my head but just don't know how to word it in code.

sectionTopicArray
[0] Dictionary 1 with key Tracks
    Track1
    Track2
    Track3
[1] Dictionary 2 with key Tracks
    Track4
[2] Dictionary 3 with key Tracks
    Track5
    Track6

basically I'm thinking...
Section0 = Dictionary1 = Topic1 
Section1 = Dictionary2 = Topic2 
Section2 = Dictionary3 = Topic3
A: 

I would use nested Arrays.

Create the DataArray:

    NSArray *dataArray = [NSArray arrayWithObjects:
                      [NSArray arrayWithObjects:    // Section 0
                       track1,                      //  Item 0
                       track2,                      //  Item 1
                       track3,                      //  Item 2  (indexpath 0.2)
                       nil],
                      [NSArray arrayWithObjects:    // Section 1
                       track4,                      //  Item 0  (indexpath 1.0)
                       nil],
                      [NSArray arrayWithObjects:    // Section 2
                       track5,                      // Item 0
                       track6,                      // Item 1   (indexpath 2.1)
                       nil],
                      nil];

Access Object in cellForRowAtIndexPath:

Track *trackAtIndexPath = [[dataArray objectAtIndex:indexpath.section] objectAtIndex:indexpath.row];

You can create section titles somehow like this:

    NSArray *sectionTitles = [NSArray arrayWithObjects:
                          @"Section 0",
                          @"Section 1",
                          @"Section 2",
                          nil];

Should be clear how to access them.

fluchtpunkt
@fluchtpunkt Thanks for the reply. Hmm...sure, I have also thought about that. But how would you do that in a loop. Since my data is not static and can change if the XML gets changed or updated.
slowman21
you just use NSMutableArray instead of those NSArrays, loop through your data and add it at the right location. You could use `[[dataArray objectAtIndex:section] addObject:track]` or something like this to add the tracks to your sections. And if you have to add data to a section that is not already in you dataArray you just add the corresponding Section-Array `[dataArray addObject:[NSMutableArray array]];`
fluchtpunkt