【问题标题】:reload table view when Contact Added swift联系人添加时重新加载表格视图
【发布时间】:2019-11-16 07:38:50
【问题描述】:

我使用 ContactUI FrameWork 来获取、编辑、创建新的 Conatacts。 VC1 中有一个表格视图来显示联系人,还有另一个视图控制器用于编辑或创建新联系人。问题是 VC1 中的表视图在 VC 2 中编辑或创建联系人时不更新。

型号:

class ContactStruct : NSObject {
    let identifier : String
    let thumbnailImageData : UIImage?
    let givenName : String
    let familyName : String
    let phoneNumbers : String
    let emailAddresses : String

    init(identi:String,img:UIImage?,name:String,family:String,phone:String,email:String) {
        self.identifier = identi
        self.thumbnailImageData = img
        self.givenName = name
        self.familyName = family
        self.phoneNumbers = phone
        self.emailAddresses = email
    }
    class func generateModelArray() -> [ContactStruct]{
         let contactStore = CNContactStore()
         var contactsData = [ContactStruct]()

         let key = [CNContactGivenNameKey,CNContactFamilyNameKey,CNContactImageDataKey,CNContactThumbnailImageDataKey,CNContactPhoneNumbersKey,CNContactEmailAddressesKey,CNLabelPhoneNumberMobile] as [CNKeyDescriptor]
         let request = CNContactFetchRequest(keysToFetch: key)
         try? contactStore.enumerateContacts(with: request, usingBlock: { (contact, stoppingPointer) in
             let givenName = contact.givenName
             let familyName = contact.familyName
             let emailAddress = contact.emailAddresses.first?.value ?? ""
             let phoneNumber = contact.phoneNumbers.first?.value.stringValue ?? ""
             let identifier = contact.identifier
            var image : UIImage?
             if contact.thumbnailImageData != nil{
                 image = UIImage(data: contact.thumbnailImageData!)!
             }
             contactsData.append(ContactStruct(identi: identifier, img: image, name: givenName, family: familyName, phone: phoneNumber, email: emailAddress as String))
         })
         return contactsData
     }
}

我以多种方式在 VC1 中重新加载表视图,但它不起作用。当我 再次运行应用表视图,显示更改正常。

在 VC1 中重新加载表格视图的方法:

VC2 中的委托:

    protocol InsertContactViewControllerDelegate {
        func applyingFor_reloadTableView()
    }

class InsertContactViewController: UIViewController {

var delegate : InsertContactViewControllerDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()
}
   @IBAction func btnUpdate_DidTouch(_ sender: Any) {
        if let delegate = self.delegate {
            delegate.applyingFor_reloadTableView()
        }
        modifyContact(contactIdentifier: stridentifier)
        self.navigationController?.popViewController(animated: true)
    }

在 vc1 中:

 class NewContactViewController:UIViewController, 

BackGroundViewControllerDelegate {
   @IBOutlet weak var tblMain: UITableView!

       func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          let story = UIStoryboard(name: "Main", bundle: nil)
          let vc = story.instantiateViewController(withIdentifier: "InsertContactViewController") as! InsertContactViewController
         vc.delegate = self
          self.navigationController?.pushViewController(vc, animated: true)
       }

      func applyingFor_reloadTableView() {
             self.tblMain.reloadData()
       }

    }

修改联系方式,我将联系标识符传递给VC2进行更新:

   func modifyContact(contactIdentifier:String) {
        let key = [CNContactGivenNameKey,CNContactFamilyNameKey,CNContactImageDataKey,CNContactThumbnailImageDataKey,CNContactEmailAddressesKey,CNContactPhoneNumbersKey] as [CNKeyDescriptor]
        let contact = try?  contactStore.unifiedContact(withIdentifier: contactIdentifier, keysToFetch: key)

        let mutableContact = contact?.mutableCopy() as! CNMutableContact
        let indexpath0 = IndexPath(row: 0, section: 0)
        let cell = tblInsert.cellForRow(at: indexpath0) as! InsertTableCell0
        let name = cell.txtFirstName.text
        let family = cell.txtLastName.text
        mutableContact.givenName = name!
        mutableContact.familyName = family!
        let indexpath1 = IndexPath(row: 0, section: 1)
        let cell1 = tblInsert.cellForRow(at: indexpath1) as! InsertTableCell1
        let phone : String = cell1.txtPhoneNumber.text!
        mutableContact.phoneNumbers = [CNLabeledValue(label: CNLabelPhoneNumberMobile, value:CNPhoneNumber(stringValue: phone))]
        let indexpath2 = IndexPath(row: 0, section: 2)
        let cell2 = tblInsert.cellForRow(at: indexpath2) as! InsertTableCell2
        let email : String = cell2.txtEmail.text!
        mutableContact.emailAddresses = [CNLabeledValue(label: CNLabelHome, value: email as NSString)]
        let saveRequest = CNSaveRequest()
        saveRequest.update(mutableContact)
        try! contactStore.execute(saveRequest)
    }

【问题讨论】:

  • 能否请您添加更新tableView的方法,
  • 我添加了那个方法

标签: ios swift uitableview contactsui


【解决方案1】:

在更新联系人之前,您正在重新加载 VC1 tableView。 首先更新联系人并重新加载tableView

代码:

 @IBAction func btnUpdate_DidTouch(_ sender: Any) {

        modifyContact(contactIdentifier: stridentifier)
        if let delegate = self.delegate {
            delegate.applyingFor_reloadTableView()
        }
        self.navigationController?.popViewController(animated: true)
    }


 func applyingFor_reloadTableView() {
       tableViewDataSource = ContactStruct.generateModelArray() // used your dataSource variable instead of tableViewDataSource.
       self.tblMain.reloadData()
 }

【讨论】:

  • 我添加了那个方法
  • 好的,您保存了联系人,但没有更新本地联系人列表,同时更新您的本地联系人列表。
  • ContactStruct.generateModelArray() 在重新加载tableView之前调用这个方法,检查我更新的代码。
猜你喜欢
  • 1970-01-01
  • 2016-09-24
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 2014-07-12
  • 2018-04-12
相关资源
最近更新 更多