RETURN AN ARRAY FOR SECTION TITLES IN TABLE VIEW

Jessica James

Total Post:16

Points:114
Posted by  Jessica James
 509  View(s)
Ratings:
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?

  1. Tarun Kumar

    Post:214

    Points:1642
    Re: return an array for section titles in table view

    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]

    sortedArrayUsingSelector:@selector(

    caseInsensitiveCompare:)];

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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!