UILocalizedIndexedCollat​​ion UITableView索引列表排序

时间:2015-03-03 14:23:54

标签: ios xcode uitableview selector uilocalizedcollation

我正在创建一个包含用户联系人列表的UITableView。我已经成功导入了联系人列表,并将它们存储在一个数组中作为字典,其中包含每个联系人的姓氏,姓氏& ID参考。我试图实现UITableView's索引列表功能。

尝试将数组分区到表索引UILocalizedIndexedCollation的不同部分时,我遇到了问题。具体由选择器。我希望能够选择是否按名字或姓氏对名称进行排序。

- (void)getContacts
{
    // Do any additional setup after loading the view, typically from a nib.
    _addressBook = ABAddressBookCreateWithOptions(NULL, NULL);

    ABAddressBookRequestAccessWithCompletion(_addressBook, ^(bool granted, CFErrorRef error) {
        NSLog(@"GRANTED");
    });

    NSArray *rawContactData = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(_addressBook);
    NSMutableArray *newContactList = [[NSMutableArray alloc] init];

    for (int i = 0; i < [rawContactData count]; i++) {
        ABRecordRef contact = (__bridge ABRecordRef)rawContactData[i];

        NSDictionary *contactData = @{ @"name" : (__bridge_transfer NSString *)ABRecordCopyValue(contact, kABPersonFirstNameProperty),
                                       @"surname" : (__bridge_transfer NSString *)ABRecordCopyValue(contact, kABPersonLastNameProperty),
                                       @"recordID" :  [NSNumber numberWithInt:ABRecordGetRecordID(contact)]};
        [newContactList addObject:contactData];
    }


    _tableData = [NSMutableArray arrayWithArray:[self partitionObjects:newContactList collationStringSelector:@selector(name)]];
}



-(NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector

{
    UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];

    NSInteger sectionCount = [[collation sectionTitles] count]; //section count is take from sectionTitles and not sectionIndexTitles
    NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount];

    //create an array to hold the data for each section
    for(int i = 0; i < sectionCount; i++)
    {
        [unsortedSections addObject:[NSMutableArray array]];
    }

    //put each object into a section
    for (NSMutableDictionary *object in array) {
        NSInteger index = [collation sectionForObject:object collationStringSelector:selector];
        [[unsortedSections objectAtIndex:index] addObject:object];
    }


    NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionCount];

    //sort each section
    for (NSMutableArray *section in unsortedSections)
    {
        [sections addObject:[collation sortedArrayFromArray:section collationStringSelector:selector]];
    }

    return sections;
}

1 个答案:

答案 0 :(得分:0)

选择器被发送到您要在数组中排序的Object。在您的情况下,您的对象是类型字典,它不实现选择器“名称”或姓氏等。您应该创建一个简单的类而不是使用字典。并根据您的喜好声明属性

@interface Contact : NSObject 
@property (strong,nonatomic) NSString  *fname; 
@property (strong,nonatomic) NSString  *lname; 
@property (strong,nonatomic) NSNumber *rerordId; 
@end

然后,您可以使用此对象而不是您正在使用的字典。

Contact *contact= [[Contact alloc]init];
contact.fname = (__bridge_transfer NSString *)
               ABRecordCopyValue(contact, kABPersonFirstNameProperty);
contact.lname = (__bridge_transfer NSString *)
               ABRecordCopyValue(contact, kABPersonLastNameProperty);
contact.recrordId = [NSNumber numberWithInt:ABRecordGetRecordID(contact)].stringValue;
[newContactList addObject:contact];

然后您可以使用任何名称或属性选择器。就像在你的例子中一样,你将使用fname编写函数调用排序,例如像

_tableData = [NSMutableArray arrayWithArray:
             [self partitionObjects:newContactList
             collationStringSelector:@selector(fname)]];