请原谅我的无知,但我主要从事游戏编程,并没有太多像应用程序这样的实用程序。我想要做的是检索用户联系人并在应用程序启动时将它们显示在表格 View 中。我不是一个完全的菜鸟,我知道如何使用 Address Book API。我只是不确定如何完成将用户联系人放入我自己的自定义 UITableView 的任务。任何帮助是极大的赞赏!
最佳答案
我不确定我的回复会有多好,因为我没有使用地址簿 API,但通过一些搜索和我自己的知识得出以下建议:
创建地址簿
执行此操作的一种方法(基于我的搜索)似乎如下:
ABAddressBookRef addressBook = ABAddressBookCreate( );
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );
for ( int i = 0; i < nPeople; i++ )
{
ABRecordRef ref = CFArrayGetValueAtIndex( allPeople, i );
...
}
显示地址簿
使用上面的代码,您现在需要将其显示在表格中。我不知道您对 UITableView
的使用有多熟悉,所以我会比我可能需要的更冗长。您将填充 tableView 类的以下方法:
- (NSInteger)numberOfSections
{
//You can also return 26 here, but you'll need to split your addressBook array, so each section contains the people starting with a single letter of the alphabet
return 1;
}
- (NSInteger)numberOfRowsInSection:(NSInteger)section
{
return [addressBook count];
}
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//do standard cell instantiation
//get the contact at index indexPath.row
//set the cell's textLabel.text to that contact's name (or whatever you want)
}
我不知道我的回答有多大帮助,但如果你想让我澄清一些事情(或者告诉我我认为你的问题如此简单是愚蠢的),请随时告诉我。
关于xcode - 如何检索用户联系人并将其显示在 UITableView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9944686/