It is forgone conclusion that if you need to present a large amount of data with a UITableView then you should do so using an index and index sections. As with most tutorials that I have read on this topic, the scenarios used got me 80% of the way. In my humble opinion, the solutions where either over engineered or they didn't apply to my use case. Hopefully this tutorial/sample project will bridge the gap.
The tutorial that I got the most from is:
iPhone Development: Creating Native Contacts like screen.
One of the biggest hurdles that I had to over come, that was different from the contacts tutorial, was that I wasn't using a database and therefore didn't have a predefined sub-result set for each character. I was having to do the opposite. I had to iterate over my result set, array of states, grab the first letter from each item, check to see if there was already a dictionary key that existed for that letter and proceed accordingly.
Logic Flow Iterate over states
Get the first letter of that particular state
Check to see if there is a key in the
objectsForCharacter dictionary
-
YES: add the state string to the
arrayOfNames array and then set the
arrayOfNames to the
objectsForCharacter dictionary for the appropriate key
-
NO: remove all objects from the
arrayOfNames array and add the
firstLetter to the
arrayOfCharacters. Then proceed on to the above "YES" steps
Once all the states have been "proceed" then reload the table data
Final Model
Though it sounds some what complicated with all the arrays, dictionarys, etc. once you break it down to a visual model then the code is easier to comprehend.
[[objectsForCharacters objectForKey:@"A"] objectAtIndex:0]; // Alabama[[objectsForCharacters objectForKey:@"A"] objectAtIndex:1]; // Alaska[[objectsForCharacters objectForKey:@"A"] objectAtIndex:2]; // Arizona[[objectsForCharacters objectForKey:@"A"] objectAtIndex:3]; // ArkansasDownload Example Project (
comments/enhancements are welcomed)
IndexedTable.zip