xcode - 如何检索用户联系人并将其显示在 UITableView 中?

标签 xcode uitableview addressbook

请原谅我的无知,但我主要从事游戏编程,并没有太多像应用程序这样的实用程序。我想要做的是检索用户联系人并在应用程序启动时将它们显示在表格 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/

相关文章:

在 Xcode 中的一个项目中创建两个子项目

ios - 将托管对象从 uitableview 传递到文本字段以在核心数据模型中进行编辑

ios - 动态更新 UITableView 高度和数据

ios - UITable,如何在 Swift IOS 中将委托(delegate)分配给 UITable

ios - 地址簿 iOS 中的内存泄漏问题

objective-c - 在iPhone的 objective-c 中导入VCard

ios - Xcode 11.4 Beta错误“无效的工具链”

ios - 创建IOS模拟器构建但缺少预制件

objective-c - Xcode——在项目中寻找死方法

ios - 从地址簿问题中获取号码