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"
);
}