Reputation: 932
I have a NSMutableArray *objectsArray;
containing dictionaries, each NSDictionary
have a "Name"
string with the objects name as value (no surprise). UITableView
is already sorted using NSSortDescriptor
by name in ascending order.
Now I need to section
the TableView using the name's first letter, but I cannot find the needed code for:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
/*Return: How many letters from a-z are represented
as the first letter of the objects name?
I.e. If there is no object in the dictionary with name starting with letter B,
the number of sections are now 25 if all the other letters are included.*/
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//Return: How many objects are starting with each letter
So I need to know how to return the correct number of section and rows?
Ps. Is there anything else that I have to do to make this it work, like in cell creation process cellForRowAtIndexPath
or when passing the objectsArray
through the segue
to the DetailViewController
?
Additional info about app structure:
objectsArray is from a plist array with dictionaries. using a custom prototype cell to populate the Table View. Xcode 4.2 with storyboard. App for iPhone 5.1
Upvotes: 0
Views: 72
Reputation: 21902
Create a NSMutableSet
before you sort. Have your comparator store, in your set, the first letter of each Name it encounters as it sorts. Then the number of sections is the size of your set. This way, you pay very little cost since your comparators are already having to iterate through the array anyway.
Upvotes: 1