Convert table view to partitions

I have a table view that has a data source from an array that contains the names of people.

Now, to simplify the search for people, I want to split the table view so that it has the letter AZ on the right side, like the address book application.

But my current array just contains a collection NSStrings. How to break them so that they are grouped by the first letter of names? Is there any convenient way to do this?

EDIT: If anyone is interested in my final code:

NSMutableArray *arrayChars = [[NSMutableArray alloc] init];

for (char i = 'A'; i <= 'Z' ; i++) {
    NSMutableDictionary *characterDict = [[NSMutableDictionary alloc]init];
    NSMutableArray *tempArray = [[NSMutableArray alloc] init];

    for (int k = 0; k < [myList count]; k++) {

        NSString *currentName = [[friends objectAtIndex:k] objectForKey:@"name"];
        char heading = [currentName characterAtIndex:0];
        heading = toupper(heading);

        if (heading == i) {
            [tempArray addObject:[friends objectAtIndex:k]];
        }
    }
    [characterDict setObject:tempArray forKey:@"rowValues"];
    [characterDict setObject:[NSString stringWithFormat:@"%c",i] forKey:@"headerTitle"];
    [arrayChars addObject:characterDict];

    [characterDict release];
    [tempArray release];
}

At the end of the function I will have:

arrayChars [0] = dictionary(headerTitle = 'A', rowValues = {"adam", "alice", etc})
arrayChars[1] = dictionary(headerTitle = 'B', rowValues = {"Bob", etc})

Thank you all for your help!

+5
source share
1

, , , nil

NSArray *names = @[@"javier",@"juan", @"pedro", @"juan", @"diego"];
NSArray *letters = @[@"j", @"p", @"d"];
NSMutableArray *objects = [[NSMutableArray alloc] init];

for (NSInteger i = 0; i < [letters count]; ++i)
{
    [objects addObject:[[NSMutableArray alloc] init]];
}

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:objects forKeys:letters];

,

for (NSString *name in names) {
    NSString *firstLetter = [name substringToIndex:1];

    for (NSString *letter in letters) {
        if ([firstLetter isEqualToString:letter]) {
            NSMutableArray *currentObjects = [dictionary objectForKey:letter];
            [currentObjects addObject:name];
        }
    }

}

,

NSLog(@"%@", dictionary);

+2

All Articles