创建一个用索引排序的联系人的tableviewcontroller

时间:2017-07-17 08:36:51

标签: ios objective-c uitableview

你好,我是IOS dev的新手,我需要创建像view这样的联系人,如果我已经有了联系人和索引的数组,我能够创建它,但我需要的是从服务器获取联系人并使用具有客观C的索引对它们进行排序,有任何帮助吗?

我查看了本教程:

     animals = @{@"B" : @[@"Bear", @"Black Swan", @"Buffalo"],
                    @"C" : @[@"Camel", @"Cockatoo"],
                    @"D" : @[@"Dog", @"Donkey"],
                    @"E" : @[@"Emu"],
                    @"G" : @[@"Giraffe", @"Greater Rhea"],
                    @"H" : @[@"Hippopotamus", @"Horse"],...
sectionTitles = [[animals allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    animalIndexTitles = @[@"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"];

但显然这不是我需要的,

1 个答案:

答案 0 :(得分:0)

首先,你必须对所有键进行排序:

NSArray *keys = [animals allKeys];
NSArray *sortedKeys = [keys sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
    NSString *first = [someDictionary objectForKey:a];
    NSString *second = [someDictionary objectForKey:b];
    return [first compare:second];
}];

排序键后,您必须根据键对每个数组进行排序。

for( i= 0 ; i < [sortedKeys count ]; i++){
NSArray * arr = [animals objectForKey:[sortedKeys objectAtIndex: i] ];
NSArray *sortedArray = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [[obj1 valueForKey:@"value"] compare:[obj2 valueForKey:@"value"]];
}];