gpt4 book ai didi

ios - ABPeoplePickerNavigationController 自定义

转载 作者:行者123 更新时间:2023-11-29 12:39:38 26 4
gpt4 key购买 nike

我想使用 ABPeoplePickerNavigationController 但我想自定义 View 。我想要一些联系人的附件图像,并且我想以不同于默认方式的方式对它们进行排序。有办法吗?还是我必须创建自己的 UITableViewController

最佳答案

如果您想以这种方式自定义联系人的外观,则必须创建自己的表格 View 。例如,您可以使用以下方法提取联系人:

- (void)loadContacts
{
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();

if (status == kABAuthorizationStatusDenied) {
// if you got here, user had previously denied/revoked permission for your
// app to access the contacts, and all you can do is handle this gracefully,
// perhaps telling the user that they have to go to settings to grant access
// to contacts

[[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
return;
}

CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);

if (error) {
NSLog(@"ABAddressBookCreateWithOptions error: %@", CFBridgingRelease(error));
if (addressBook) CFRelease(addressBook);
return;
}

ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (error) {
NSLog(@"ABAddressBookRequestAccessWithCompletion error: %@", CFBridgingRelease(error));
}

dispatch_async(dispatch_get_main_queue(), ^{
if (granted) {
// if they gave you permission, then get copy of contacts and reload table

self.allContacts = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, NULL, kABPersonSortByLastName));

[self.tableView reloadData];
} else {
// however, if they didn't give you permission, handle it gracefully, for example...

[[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}

if (addressBook) CFRelease(addressBook);
});
});
}

然后您可以在 cellForRowAtIndexPath 中使用此数组:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

ABRecordRef person = (__bridge ABRecordRef)self.allContacts[indexPath.row];

NSMutableArray *nameArray = [NSMutableArray array];

NSString *prefix = CFBridgingRelease(ABRecordCopyValue(person, kABPersonPrefixProperty));
if (prefix) [nameArray addObject:prefix];

NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
if (firstName) [nameArray addObject:firstName];

NSString *middleName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonMiddleNameProperty));
if (middleName) [nameArray addObject:middleName];

NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
if (lastName) [nameArray addObject:lastName];

NSString *fullname = [nameArray componentsJoinedByString:@" "];

NSString *suffix = CFBridgingRelease(ABRecordCopyValue(person, kABPersonSuffixProperty));
if (suffix) {
fullname = [NSString stringWithFormat:@"%@, %@", fullname, suffix];
}

cell.textLabel.text = fullname;

NSString *company = CFBridgingRelease(ABRecordCopyValue(person, kABPersonOrganizationProperty));
if ([fullname length] == 0) {
cell.textLabel.text = company;
cell.detailTextLabel.text = nil;
} else {
cell.detailTextLabel.text = company;
}

if ([nameArray count] == 0 && [company length] == 0)
NSLog(@"nothing to show!!!");

return cell;
}

显然,鉴于整个想法是您想要自定义单元格,相应地修改 cellForRowAtIndexPath,但希望这能说明这个想法。

关于ios - ABPeoplePickerNavigationController 自定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25309088/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com