Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort contact alphabetically with Section UITableView

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"
    );
}
like image 819
Vjardel Avatar asked Aug 01 '15 21:08

Vjardel


1 Answers

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. ;)

like image 95
Aish Avatar answered Oct 04 '22 21:10

Aish