按字母顺序对 UITableView 内容(地址簿)进行排序
Sort UITableView content (Address Book) alphabetically
下面是我用来从设备中检索联系人列表的内容。我希望它按字母顺序显示,但使用在堆栈溢出中看到的其他示例我无法让它工作。
下面的代码来自一个教程,我需要做什么才能按照字母顺序排序?
- (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
但我没有排序依据的键。
您需要实现并提供一种方法来对 Person 记录进行排序,作为 sortUsingSelector 方法调用的选择器。
您需要对 Person
个对象的数组进行排序。将它们全部添加到数组后,您可以使用以下代码对 fullName
进行排序:
[self.tableData sortUsingComparator:^NSComparisonResult(Person *p1, Person *p2) {
return [p1.fullName compare:p2.fullName];
}];
备选
您可能希望在 Person
对象上实现一个 compare:
方法并在那里执行比较,这将很好地封装排序逻辑并确保使用 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:)];
我是这样解决的
//具有获取属性的键 NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactEmailAddressesKey]; CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
//按姓氏排序联系人。 request.sortOrder = CNContactSortOrderFamilyName;
--或者你可以--
//按姓名排序联系人。 request.sortOrder = CNContactSortOrderGivenName;
下面是我用来从设备中检索联系人列表的内容。我希望它按字母顺序显示,但使用在堆栈溢出中看到的其他示例我无法让它工作。 下面的代码来自一个教程,我需要做什么才能按照字母顺序排序?
- (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
但我没有排序依据的键。
您需要实现并提供一种方法来对 Person 记录进行排序,作为 sortUsingSelector 方法调用的选择器。
您需要对 Person
个对象的数组进行排序。将它们全部添加到数组后,您可以使用以下代码对 fullName
进行排序:
[self.tableData sortUsingComparator:^NSComparisonResult(Person *p1, Person *p2) {
return [p1.fullName compare:p2.fullName];
}];
备选
您可能希望在 Person
对象上实现一个 compare:
方法并在那里执行比较,这将很好地封装排序逻辑并确保使用 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:)];
我是这样解决的
//具有获取属性的键 NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactEmailAddressesKey]; CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
//按姓氏排序联系人。 request.sortOrder = CNContactSortOrderFamilyName;
--或者你可以--
//按姓名排序联系人。 request.sortOrder = CNContactSortOrderGivenName;