我添加联系人后表格视图没有更新,在表格视图上所有联系人都在设备中获取

问题描述 投票:0回答:0

这是我的视图控制器类`

在这个类中,按钮下面的第 3 行,从目录加载所有联系人,我需要重新加载 添加联系人后的表格,但它没有更新,但每当我重新启动应用程序时它都会更新


import UIKit
import SwifterSwift
import Contacts
import ContactsUI
class tableView4: UITableViewCell{
    
    var contactReloadDelegate: contactReload?
    
    @IBOutlet weak var btnAddContact: UIButton!
    @IBAction func btnClick(_ sender: Any) {
        
        let vc = UIStoryboard.main?.instantiateViewController(withIdentifier: "saveContact") as! saveContact
        self.parentViewController?.navigationController?.present(vc, animated: true, completion: nil)
        
    }
    
}
class ViewController: UITableViewController, contactReload{
    
    func funcContactRel() {
        DispatchQueue.main.async {
          self.tableView.reloadData()
            //self.contactDisplay()
        }

        //tableView.reloadData()
    }
    var contacts = [CNContact]()
    var saveCon = saveContact()
    let store = CNContactStore()
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
       
        tableView.delegate = self
        tableView.dataSource = self
        //tableView.reloadData()
        DispatchQueue.main.async {
            self.contactDisplay()
           // self.tableView.reloadData()
        }
        
        tableView.register(UINib(nibName: "FirstCell", bundle: nil), forCellReuseIdentifier: "cell1")
        tableView.register(UINib(nibName: "SecondCell", bundle: nil), forCellReuseIdentifier: "SecondCell")
        tableView.register(UINib(nibName: "ThirdCell", bundle: nil), forCellReuseIdentifier: "ThirdCell")
        
        
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("Tapped")
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 4
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return 1
        }
        else if section == 1{
            return 1
        }
        if section == 2{
            return 1
        }
        else{
            print("count while reload data = \(contacts.count)")
            return contacts.count
        }
    }
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            if indexPath.section < 2{
                return 130
            }  else {
                return 50
            }
            
        }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
             
        
        if indexPath.section == 0{
            let cell1 = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! FirstCell
            
            let imageHeight = cell1.myImage.height
            cell1.myImage.roundCorners(.allCorners, radius: (imageHeight)/2)
            
            return cell1
           //cell1.textLabel?.text = "Hello"
        }
        else if indexPath.section == 1{
            let cell2 = tableView.dequeueReusableCell(withIdentifier: "CellViewForCollectionView", for: indexPath) as! CellViewForCollectionView

            
            return cell2
        }
        else if indexPath.section == 2{
            let cell4 = tableView.dequeueReusableCell(withIdentifier: "cell4", for: indexPath) as! tableView4
            return cell4
        }
        else{
            let contact = contacts[indexPath.row]
            
            if let cell3 = tableView.dequeueReusableCell(withIdentifier: "cell3", for: indexPath) as? UITableViewCell{

            cell3.textLabel?.text = "\(contact.givenName)"
            cell3.detailTextLabel?.text = contact.phoneNumbers.first?.value.stringValue
            print("record  = \(indexPath.row) , name = \(contact.givenName)")
                return cell3
            }else{
                print("not getting cell for record = \(indexPath.row)")
                return UITableViewCell()
            }
            
        }
        
        //return UITableViewCell(
    }
    
    func contactDisplay(completion: (() -> Void)? = nil) {
        store.requestAccess(for: .contacts) { granted, error in
            guard granted else {
                DispatchQueue.main.async {
                    let alertController = UIAlertController(title: "Permission Denied", message: "Please allow access to contacts in Settings", preferredStyle: .alert)
                    alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                    self.present(alertController, animated: true, completion: nil)
                }
                return
            }
            let keys = [CNContactGivenNameKey, CNContactPhoneNumbersKey]
            let request = CNContactFetchRequest(keysToFetch: keys as [CNKeyDescriptor])
            self.contacts.removeAll()
            do {
                try self.store.enumerateContacts(with: request) { contact, stop in
                    self.contacts.append(contact)
                }
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                    completion?()
                }
            } catch {
                print(error)
            }
        }
    }
}

这是我的保存联系人类

需要用户名和手机号码详细信息


import Foundation
import ContactsUI
protocol contactReload {
    func funcContactRel()
}
class saveContact: UIViewController{
    var saveData = false
   
    @IBOutlet weak var contactName: UITextField!
    @IBOutlet weak var contactPhone: UITextField!
    override func viewDidLoad() {
        
    }
    
    @IBAction func saveContact(_ sender: UIButton) {
        
        let store = CNContactStore()
        let contact = CNMutableContact()

        contact.givenName = contactName.text ?? ""
        
        contact.phoneNumbers.append(CNLabeledValue(
            label: "mobile", value: CNPhoneNumber(stringValue: contactPhone.text ?? "")))

        let saveRequest = CNSaveRequest()
        saveRequest.add(contact, toContainerWithIdentifier: nil)
        try? store.execute(saveRequest)
        let viewControllerobj = ViewController()
        if saveRequest != nil{
            
            //viewControllerobj.contactDisplay()
            viewControllerobj.funcContactRel()
            self.saveData = true
        } else {
            self.saveData = false
        }
        self.dismiss(animated: true) {
           
            
            print("dismiss")
        }
    }
}

我试图在添加联系人后加载记录,但表没有重新加载,我的联系人数组中也有数据

ios swift uitableview tableview
© www.soinside.com 2019 - 2024. All rights reserved.