Reputation: 591
I would like to know how to make a alphabet group sections for the names extracted from sqlite database in iphone (xcode 4.2). I was able to do group section for first name but was unable to do the last name part. Some name have lowercase I want the headers letter to have all capital which includes the lower and uppercase letters of those names. Some names have 3 names and wanted to make sure i get the last name. My code is below and it seems to mix up the alphabet (from A to L to C to D) and also e and E (lower and upper which i dont want to display)
//---create the index---
nameIndex = [[NSMutableArray alloc] init];
for (int i=0; i<[_authorsInfo count]-1; i++){ //---get the first char of each state--- authors *tempAuthor = [_authorsInfo objectAtIndex:i]; char alphabet = [tempAuthor.name characterAtIndex:0]; NSString *uniChar = [NSString stringWithFormat:@"%C", alphabet]; //---add each letter to the index array--- if (![nameIndex containsObject:uniChar]) { [nameIndex addObject:uniChar]; }
UPDATE*
self.authorsInfo = [poemDatabase database].authorsInfo;
//---create the index--- nameIndex = [[NSMutableArray alloc] init]; for (int i=0; i<[_authorsInfo count]-1; i++){ //---get the first char of each state--- authors *tempAuthor = [_authorsInfo objectAtIndex:i]; NSArray *nameComponents = [tempAuthor.name componentsSeparatedByString:@" "]; NSString *lastName = [ nameComponents objectAtIndex: nameComponents.count-1 ]; char alphabet = [lastName characterAtIndex:0]; //char alphabet = [tempAuthor.name characterAtIndex:0]; NSString *uniChar = [NSString stringWithFormat:@"%C", alphabet]; //---add each letter to the index array--- if (![nameIndex containsObject:uniChar]) { [nameIndex addObject:uniChar]; } }
Upvotes: 2
Views: 1198
Reputation: 29767
If tempAuthor.name
contains whole name with spaces try this:
NSArray *nameComponents = [tempAuthor.name componentsSeparatedByString:@" "];
NSString *lastName = [ nameComponents objectAtIndex: nameComponents.count-1 ]
char alphabet = [lastName characterAtIndex:0];
Upvotes: 2