1

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

尝试将数组分区到表 index 中的不同部分时,我遇到了问题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;
}
4

1 回答 1

0

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

@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)]];
于 2015-10-15T19:42:09.477 回答