我只想创建带有索引部分的自定义连接列表(索引列表必须与默认联系人列表相同)。例如,如果我创建任何没有姓名的联系人,然后带有 # index ,如果任何联系人以数字开头,那么它也带有#index。我正在使用下面的代码,但没有运气。
func retrieveContactsWithStore(store: CNContactStore){
let keysToFetch =
[CNContactFormatter.descriptorForRequiredKeys(for: .fullName),
CNContactPhoneNumbersKey,CNContactImageDataKey,
CNContactEmailAddressesKey] as [Any]
let request = CNContactFetchRequest(keysToFetch: keysToFetch as!
[CNKeyDescriptor])
request.sortOrder = CNContactSortOrder.userDefault
var cnContacts = [CNContact]()
do {
try store.enumerateContacts(with: request){
(contact, cursor) -> Void in
if (!contact.phoneNumbers.isEmpty) {
}
if contact.isKeyAvailable(CNContactImageDataKey) {
if let contactImageData = contact.imageData {
print(UIImage(data: contactImageData) ?? "")
}
} else {
// No Image available
}
if (!contact.emailAddresses.isEmpty) {
}
cnContacts.append(contact)
self.contacts = cnContacts
}
} catch let error {
NSLog("Fetch contact error: \(error)")
}
NSLog(">>>> Contact list:")
DispatchQueue.main.async {
for contact in self.contacts {
let fullName = CNContactFormatter.string(from: contact, style: .fullName) ?? "No Name"
let contactKey = String(fullName.prefix(1))
if var contactValues = self.contactsDictionary[contactKey] {
contactValues.append(contact)
self.contactsDictionary[contactKey] = contactValues
} else {
self.contactsDictionary[contactKey] = [contact]
}
}
// 2
self.contactsSectionTitles = [String](self.contactsDictionary.keys)
self.contactsSectionTitles = self.contactsSectionTitles.sorted(by: { $0 < $1 })
self.tableView.reloadData()
}
}
func numberOfSections(in tableView: UITableView) -> Int {
// 1
return contactsSectionTitles.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection
section: Int) -> String? {
return contactsSectionTitles[section]
}
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return contactsSectionTitles
}
得到了解决方案,我点击了这个链接:https : //developer.apple.com/documentation/uikit/uilocalizedindexedcollation
class ObjectTableViewController: UITableViewController {
let collation = UILocalizedIndexedCollation.currentCollation()
var sections: [[AnyObject]] = []
var objects: [AnyObject] = [] {
didSet {
let selector: Selector = "localizedTitle"
sections = Array(count: collation.sectionTitles.count, repeatedValue: [])
let sortedObjects = collation.sortedArrayFromArray(objects, collationStringSelector: selector)
for object in sortedObjects {
let sectionNumber = collation.sectionForObject(object, collationStringSelector: selector)
sections[sectionNumber].append(object)
}
self.tableView.reloadData()
}
}
// MARK: UITableViewDelegate
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String {
return collation.sectionTitles[section]
}
override func sectionIndexTitlesForTableView(tableView: UITableView) -> [String] {
return collation.sectionIndexTitles
}
override func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
return collation.sectionForSectionIndexTitleAtIndex(index)
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句