Home > DeveloperSection > Forums > return an array for section titles in table view
Jessica James
Jessica James

Total Post:16

Posted on    November-18-2015 5:25 AM

 iPhone iPhone  Ios  Objective C 

 1 Reply(s)
 279  View(s)
Rate this:
I want to create an array for section titles because section titles are the first letter for the items in the section.
like contact list on device, that displays in section title like 'A' and after displaying contacts which are started from character 'A', and so on.
Here is my code that I am using in my app:
NSSortDescriptor *descriptorObj;
descriptorObj = [[[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES] autorelease];
NSArray *arrayObj;
NSMutableArray *sectionTitlesArray = [tableDataSource valueForKey:@"Name"];

NSArray *arrayUniq;
arrayUniq = [[NSSet setWithArray:sectionTitlesArray] allObjects];
arrayObj = [uniquearray arrayObjUsingSelector:@selector(caseInsensitiveCompare:)];
This code removes the duplicate titles and sorts the list alphabetically.
Now I just need to convert the strings in the array to the first letters only.
Can anyone tell me the best way to do it?

Tarun Kumar

Total Post:210

Posted on    November-18-2015 10:26 PM

You can use this code:

NSMutableSet *mutableSet = [NSMutableSet setWithCapacity:0];
for(NSString*string in [dataSource valueForKey:@"Name"])
    [mutableSet addObject:[string substringToIndex:1]];
NSArray *sectionTitleArray = [[mutableSet allObjects]



Modified On Nov-18-2015 10:28:14 PM

Don't want to miss updates? Please click the below button!

Follow MindStick