gpt4 book ai didi

ios - 未在 UITableView 中加载联系人

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:14:11 24 4
gpt4 key购买 nike

在我的 iOS 应用程序中,我有一个显示 UITableView 的 ViewController。一切(委托(delegate)、数据源...)都已正确设置。我想做一件常见的事情:用手机中的联系人填充 TableView。

但是由于某些原因,TableView 始终为空,而我在 viewDidLoad 中成功获取了联系人。我注意到在获取联系人列表之前调用了方法 tableView:numberOfRowsInSection: 但我不知道是什么问题。这是我的 Controller.m:

@interface Controller () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *contacts;
@property(nonatomic, strong) IBOutlet UITableView *contactsTable;

@end

@implementation Controller

- (void)viewDidLoad {
[super viewDidLoad];
self.contacts = [[NSMutableArray alloc] init];
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();

if (status == kABAuthorizationStatusDenied || status == kABAuthorizationStatusRestricted) {
// 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 (!addressBook) {
NSLog(@"ABAddressBookCreateWithOptions error: %@", CFBridgingRelease(error));
return;
}

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

if (granted) {
// if they gave you permission, then just carry on

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

dispatch_async(dispatch_get_main_queue(), ^{
// BTW, this is not on the main thread, so dispatch UI updates back to the main queue

[[[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];
});
}

CFRelease(addressBook);
});
}


- (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook
{
NSArray *allPeople = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
NSInteger numberOfPeople = [allPeople count];
for (NSInteger i = 0; i < numberOfPeople; i++) {
ABRecordRef person = (__bridge ABRecordRef)allPeople[i];

NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
NSString *fullname = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) {
NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i));
NSLog(@" phone:%@", phoneNumber);
GIContact *contact = [[GIContact alloc] initWithFullName:fullname telephone:phoneNumber];
[self.contacts addObject:contact];
}

CFRelease(phoneNumbers);
}
}

//Called before I got the contacts
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [self.contacts count]; //This works when I replace it with a number like 2
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Create an instance of UITableViewCell, with default appearance
UITableViewCell *cell =
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"ContactTableCell"];
// Set the text on the cell with the description of the item
// that is at the nth index of items, where n = row this cell
// will appear in on the tableview
GIContact *contact = self.contacts[indexPath.row];
cell.textLabel.text = contact.fullname;
return cell;
}

@end

Contact 是一个简单的模型类,有两个 NSString(全名和电话)

最佳答案

成功连接并检索联系人列表后,刷新表格 View 。 numberOfRowsInSection 被调用是因为 View 立即尝试显示数据,而数据还不存在。

dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});

关于ios - 未在 UITableView 中加载联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31671499/

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