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];
NS开发者_如何转开发Array *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
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.
精彩评论