【问题标题】:How to specify which cells to show in UITableView?如何指定在 UITableView 中显示哪些单元格?
【发布时间】:2015-12-08 07:19:04
【问题描述】:

目前我有 UITableView 显示联系人列表(姓名/电话号码/图像),我有来自服务器的电话号码列表。 我需要 UITableView 只显示与服务器电话号码匹配的联系人。 联系人是使用联系人框架导入的:

func findContacts() {
    let store = CNContactStore()

    let keysToFetch = [CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName),
        CNContactImageDataKey,
        CNContactPhoneNumbersKey]

    let fetchRequest = CNContactFetchRequest(keysToFetch: keysToFetch)

    do {
        try store.enumerateContactsWithFetchRequest(fetchRequest, usingBlock: { (let contact, let stop) -> Void in
            //Saves all contacts as [CNContact]
            self.contacts.append(contact)
        })
    }

例如我得到电话号码

var numberArray = [String]()
var number = contacts[indexPath.row].phoneNumbers
let phoneNumber = number.value as! CNPhoneNumber
numberArray.append(phoneNumber.stringValue)

是否可以指定 tableView 何时应该返回单元格或者我应该尝试实现for _ in _ 循环?

【问题讨论】:

    标签: ios swift uitableview contacts tableviewcell


    【解决方案1】:

    enumerateContactsWithFetchRequest 中,您应该检查电话号码是否在您从服务器获取的电话号码列表中。

    否则,您可以使用此答案中提供的方法过滤您的联系人数组:https://stackoverflow.com/a/28733586/1228075

    【讨论】:

      【解决方案2】:

      最好的方法是仅将地址加载到电话号码匹配的表格视图数据模型中 - 因此您应该先过滤从服务器获得的结果,然后再将它们显示在表格中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-08
        相关资源
        最近更新 更多