swift - 如何在 tableView 中快速制作带有联系人的字母部分

标签 swift uitableview

我是 Swift 的新手,我有使用 CNContact(familyName, givenName, phoneNumber) 制作的数组。

我想按字母顺序排列姓名联系人并将它们分组,以便将它们放在“titleForHeaderInSection”中,如下所示。

iOS contacts screenshot

有谁知道如何分组并将其放入 titleForHeaderInSection?

struct AddressModel { 
    let nameAndPhone: [AddressContact]
}

struct AddressContact {
    let contact: CNContact
}

class AddressViewController: UITableViewController {

    var addressArray = [AddressModel]()

    private func fetchContacts() {
        print("Attempting to fetch contacts today")

        let store = CNContactStore()

        store.requestAccess(for: .contacts) { (granted, err) in
            if let err = err {
                print("Failed to request access:", err)
                return
            }
            if granted {
                let keys = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName), CNContactPhoneNumbersKey] as [Any]
                let request = CNContactFetchRequest(keysToFetch: keys as! [CNKeyDescriptor])
                request.sortOrder = CNContactSortOrder.userDefault

                do {
                    var addressContact = [AddressContact]()
                    try store.enumerateContacts(with: request, usingBlock: { (contact, stop) in
                    addressContact.append(AddressContact(contact: contact))
                 })
                 let nameAndPhone = AddressModel(nameAndPhone: addressContact)
                 self.addressArray = [nameAndPhone] 
            } catch let err {
                print("Failed to enumerate contacts:", err)
            }
        } else {
            print("Access denied..")
        }
    }
}

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "Section \(section)"
}

override func numberOfSections(in tableView: UITableView) -> Int {
    return self.addressArray.count
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.addressArray[section].nameAndPhone.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

    let nameAndPhone = addressArray[indexPath.section].nameAndPhone[indexPath.row]

    let fullName = nameAndPhone.contact.familyName + nameAndPhone.contact.givenName
    cell.textLabel?.text = fullName
    return cell
}

最佳答案

试试这个

func sectionIndexTitles(for tableView: UITableView) -> [String]? {
  // ...
}

关于swift - 如何在 tableView 中快速制作带有联系人的字母部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49516326/

相关文章:

ios - 我如何优雅地从一个嵌入式 View 导航到另一个(在 IB 中使用布局)?

swift - 使用运行时指定的协议(protocol)一致性测试和选择器的通用调度机制

ios - UITableView 拖放到表外 = 崩溃

iphone - viewcontroller 容器的模拟宽度 View 小于嵌入式 tableview Controller 宽度

iphone - UITableView样式中的自定义selectedSelectedBackgroundView出站分组

ios - 使用 FBSDKGraphRequest 在 swift 中获取所有 taggable_friends 图片

Swift 3 在页面 View Controller 中嵌入按钮

swift - 使用数组和字典在 Swift 中实现多重映射

ios - 非空数组出现越界错误 - 发生了什么?

iphone - 如何从选定的 UITableViewCell 获取自定义 NSManagedObject 子类