extension ContactsTourViewController {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(“Contacts count: (contacts.count)”)
return contacts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: ContactsTableViewCell.identifier, for: indexPath) as? ContactsTableViewCell else {
return UITableViewCell()
}
let contact = contacts[indexPath.row]
cell.nameLabel.text = "(contact.givenName) (contact.familyName)"
cell.phoneLabel.text = contact.phoneNumbers.first?.value.stringValue ?? "Error: Phone number missing"
cell.delegate = self
cell.selectionStyle = .none
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) as? ContactsTableViewCell {
cell.roleTextField.becomeFirstResponder()
}
}
func didSelectRole(_ role: String?, forCell cell: ContactsTableViewCell) {
guard let indexPath = contactsTableView.indexPath(for: cell) else {
return
}
// Reload only the cell that was updated
contactsTableView.reloadRows(at: [indexPath], with: .automatic)
print("Selected role: (role ?? "None") for contact at (indexPath.row)")
}
}
I stored my contact in this property class ContactsTourViewController: UIViewController, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate, ContactsTableViewCellDelegate {
var contacts: [CNContact] = [] {
didSet {
contactsTableView.reloadData()
}
} the I fetched my contact using this function and loaded them in my table view private func fetchContacts() {
let store = CNContactStore()
store.requestAccess(for: .contacts) { granted, error in
guard granted else {
print("Access denied")
return
}
let keysToFetch = [
CNContactGivenNameKey,
CNContactFamilyNameKey,
CNContactPhoneNumbersKey,
CNContactImageDataKey
] as [CNKeyDescriptor]
let request = CNContactFetchRequest(keysToFetch: keysToFetch)
DispatchQueue.global(qos: .userInitiated).async {
var fetchedContacts: [CNContact] = []
do {
try store.enumerateContacts(with: request) { contact, stop in
fetchedContacts.append(contact)
}
DispatchQueue.main.async {[weak self] in
self?.contacts = fetchedContacts
self?.contactsTableView.reloadData()
}
} catch {
print("Failed to fetch contacts:", error)
}
}
}
}
}
extension ContactsTourViewController {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(“Contacts count: (contacts.count)”)
return contacts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: ContactsTableViewCell.identifier, for: indexPath) as? ContactsTableViewCell else {
return UITableViewCell()
}
let contact = contacts[indexPath.row]
cell.nameLabel.text = "(contact.givenName) (contact.familyName)"
cell.phoneLabel.text = contact.phoneNumbers.first?.value.stringValue ?? "Error: Phone number missing"
cell.delegate = self
cell.selectionStyle = .none
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) as? ContactsTableViewCell {
cell.roleTextField.becomeFirstResponder()
}
}
func didSelectRole(_ role: String?, forCell cell: ContactsTableViewCell) {
guard let indexPath = contactsTableView.indexPath(for: cell) else {
return
}
// Reload only the cell that was updated
contactsTableView.reloadRows(at: [indexPath], with: .automatic)
print("Selected role: (role ?? "None") for contact at (indexPath.row)")
}
}
Collins Muthomi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1