I have a plist that contains an array with a collection of dictionaries. The dictionaries in the array are used to create a fairly complex tableview with 2 images and 2 lines of text in each cell. I am wanting to create sections in the tableview based on the first letter for the value corresponding to the "MainText" key.
Here is the plist format. In this example there should be 2 sections, one for the dictionary with the "MainText" string starting with "A" and one for the 2 dictionaries with the "MainText" strings starting with "B".
<dict>
<key>Rows</key>
<array>
<dict>
<key>MainText</key>
<string>A sometext</string>
<key>SecondaryText</key>
<string>somemoretext</string>
<key>PrimaryIcon</key>
<string>firsticon.png</string>
<key>SecondaryIcon</key>
<string>secondicon.png</string>
</dict>
<dict>
<key>MainText</key>
<string>B sometext</string>
<key>SecondaryText</key>
<string>somemoretext</string>
<key>PrimaryIcon</key>
<string>firsticon.png</string>
<key>SecondaryIcon</key>
<string>secondicon.png</string>
</dict>
<dict>
<key>MainText</key>
<string>B moreTextStartingWith"B"</string>
<key>SecondaryText</key>
<string>somemoretext</string>
<key>PrimaryIcon</key>
<string>firsticon.png</string>
<key>SecondaryIcon</key>
<string>secondicon.png</string>
</dict>
</array>
I've written the code for extracting the first letters from the string at the "MainText" key, sorting them and creating the section headers. I also have the code for setting the correct number of rows in each section. Here's some of the code I've been tangling with in my viewDidLoad method.
//Create an array sorted by the strings in "MainText" of the AppDelegate.data
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"MainText" ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray;
sortedArray = [[AppDelegate.data objectForKey:@"Rows"] sortedArrayUsingDescriptors:sortDescriptors];
//Now set tableDataSource equal to sortedArray
self.tableDataSource = sortedArray;
//Create a set of the first letters used by the strings in MainText
NSMutableSet *firstCharacters = [NSMutableSet setWithCapacity:0];
for( NSString *string in [tableDataSource valueForKey:@"MainText"] )
[firstCharacters addObject:[string substringToIndex:1]];
//Create a sorted array of first letters used by strings in MainText
self.arrayOfInitialLetters = [[firstCharacters allObjects] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
//Create an array of the MainText of each item in tableDataSource
NSMutableArray * mainTextArray = [tableDataSource valueForKey:@"MainText"];
How should I go about getting the correct rows in my cellForRowAtIndexPath method? Currently each section just displays the same cells starting at the first. I need to somehow define in my data source that there are different sections. Thanks for the help this has been a real struggle!