Sort contact alphabetically with Section UITableView
Asked Answered
M

1

7

I have a NSMutableArray : self.contact with objects (name sorted alphabetically) :

(
    "Anna Haro",
    "Cheesy Cat",
    "Daniel Higgins",
    "David Taylor",
    "Freckles Dog",
    "Hank Zakroff",
    "John Appleseed",
    "Kate Be\U00e9ll"
)

I succeed to display on the right the alphabet with this line of code :

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
        return[NSArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil];
    }

Now, I've to implement the method that allows me to access on the good section ?

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index {

}

And maybe I've to change numberOfSections ? Here is my code :

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1
}

next :

I've made two Arrays : NSArray *test = [self.contact sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; :

(
    "Anna Haro",
    "Cheesy Cat",
    "Daniel Higgins",
    "David Taylor",
    "Freckles Dog",
    "Hank Zakroff",
    "John Appleseed",
    "Kate Be\U00e9ll"
)

and

NSMutableDictionary dicoAlphabet :

// Dictionary will hold our sub-arrays
    self.dicoAlphabet = [NSMutableDictionary dictionary];

    // Iterate over all the values in our sorted array
    for (NSString *value in test) {

        // Get the first letter and its associated array from the dictionary.
        // If the dictionary does not exist create one and associate it with the letter.
        NSString *firstLetter = [value substringWithRange:NSMakeRange(0, 1)];
        NSMutableArray *arrayForLetter = [self.dicoAlphabet objectForKey:firstLetter];
        if (arrayForLetter == nil) {
            arrayForLetter = [NSMutableArray array];
            [self.dicoAlphabet setObject:arrayForLetter forKey:firstLetter];
        }

        // Add the value to the array for this letter
        [arrayForLetter addObject:value];
    }

    // arraysByLetter will contain the result you expect
    NSLog(@"Dictionary: %@", self.dicoAlphabet);

returns :

Dictionary: {
    A =     (
        "Anna Haro"
    );
    C =     (
        "Cheesy Cat"
    );
    D =     (
        "Daniel Higgins",
        "David Taylor"
    );
    F =     (
        "Freckles Dog"
    );
    H =     (
        "Hank Zakroff"
    );
    J =     (
        "John Appleseed"
    );
    K =     (
        "Kate Be\U00e9ll"
    );
}
Marcelmarcela answered 1/8, 2015 at 21:42 Comment(0)
C
2

try out the following link. Will help you do this pretty easily. Let me know if you have any issues while you do this. Tutorial for contact view controller section headers and index

So for non static data, let us say you have a array of contacts called arrContacts then you can easily sort it using [arrContacts sortUsingSelector: @selector( localizedCaseInsensitiveCompare:)] now to get an array with all the section titles just loop through the array and for each object trim the string to first letter and add to this array if not already there. Thats it, now you have a section list in one array and alphabetically sorted contacts in another. Let me know how it goes. ;)

Crampton answered 6/8, 2015 at 8:24 Comment(2)
hey man, thanks for replying ! I've already seen this tuto, it's a good tuto, but it's for static data (animals), I would like to replace animals = @{@"B" : @[@"Bear", @"Black Swan", @"Buffalo"] ... with variable data (contact list) ... Do you know how to make this ?Marcelmarcela
Thanks man ! I've updated my question :) What I've to do next please ?Marcelmarcela

© 2022 - 2024 — McMap. All rights reserved.