以下是我用来从设备中检索联系人列表的内容。我希望它按字母顺序显示,但使用堆栈溢出时看到的其他示例我无法让它工作。 下面的代码来自一个教程,我需要做什么才能按字母顺序排序?
- (void)getPersonOutOfAddressBook
{
//1
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (addressBook != nil) {
NSLog(@"Succesful.");
//2
NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
//3
NSUInteger i = 0; for (i = 0; i < [allContacts count]; i++)
{
Person *person = [[Person alloc] init];
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
//4
NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson,
kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
NSString *fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
person.firstName = firstName; person.lastName = lastName;
person.fullName = fullName;
//email
//5
ABMultiValueRef emails = ABRecordCopyValue(contactPerson, kABPersonEmailProperty);
//6
NSUInteger j = 0;
for (j = 0; j < ABMultiValueGetCount(emails); j++) {
NSString *email = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(emails, j);
if (j == 0) {
person.homeEmail = email;
NSLog(@"person.homeEmail = %@ ", person.homeEmail);
}
else if (j==1) person.workEmail = email;
}
//7
[self.tableData addObject:person];
}
//8
CFRelease(addressBook);
} else {
//9
NSLog(@"Error reading Address Book");
}
}
这是我的UITableView代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
Person *person = [self.tableData objectAtIndex:indexPath.row];
cell.textLabel.text = person.fullName;
return cell;
}
我在下面试过
[self.tableData sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
我也尝试了NSSortDescriptor
,但我没有按键排序。
答案 0 :(得分:2)
您需要对Person
个对象进行排序。将所有内容添加到阵列后,您可以使用以下代码对fullName
进行排序:
[self.tableData sortUsingComparator:^NSComparisonResult(Person *p1, Person *p2) {
return [p1.fullName compare:p2.fullName];
}];
<强>替代强>
您可能希望在compare:
对象上实现Person
方法并在那里执行比较,这将使排序逻辑很好地封装,并确保使用Person
个对象的任何其他内容都可以轻松执行排序而不重复上面显示的代码。
@implementation Person
// Mostly likely this implementation will contain more code, not shown for brevity
- (NSComparisonResult)compareByFullName:(Person *)otherPerson {
return [self.fullName compare:otherPerson.fullName];
}
@end
然后你可以用:
对数组进行排序[self.tableData sortUsingSelector:@selector(compareByFullName:)];
答案 1 :(得分:0)
您需要实现并提供一种方法来将Person记录排序为sortUsingSelector方法调用的选择器。
答案 2 :(得分:0)
我设法解决了这个问题。
//具有获取属性NSArray的键* keys = @ [CNContactFamilyNameKey,CNContactGivenNameKey,CNContactEmailAddressesKey]; CNContactFetchRequest * request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
//按姓氏订购联系人。 request.sortOrder = CNContactSortOrderFamilyName;
-或者您可以-
//按名称订购联系人。 request.sortOrder = CNContactSortOrderGivenName;