如何在swift的tableView中使用联系人创建字母部分

如何在swift的tableView中使用联系人创建字母部分,swift,uitableview,Swift,Uitableview,我是Swift的新手,我用CNContact制作了数组(familyName、givenName、phoneNumber) 我想按字母顺序命名联系人,并将其分组,以便将其放在“TitleForHeaderSection”中,如下所示 有人知道如何分组并将其放入标题部分吗 struct AddressModel { let nameAndPhone: [AddressContact] } struct AddressContact { let contact: CNConta

我是Swift的新手,我用CNContact制作了数组(familyName、givenName、phoneNumber)

我想按字母顺序命名联系人,并将其分组,以便将其放在“TitleForHeaderSection”中,如下所示

有人知道如何分组并将其放入标题部分吗

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]? {
  // ...
}

要对节标题进行排序,请尝试使用闭包:

sectionTitle = sectionTitle.sorted(by: { $0 < $1 }) // First argument smaller then second argument.
sectionTitle=sectionTitle.sorted(按:{$0<$1})//第一个参数比第二个参数小。

有什么错误,您卡在哪里了?我想知道-如何按节的字母顺序对tableview中的数据进行排序。我尝试过这样做..但我不知道我必须在该函数中放入什么。。。。