【问题标题】:Looping through phone numbers, creating a custom view and passing it the phone number in swift遍历电话号码,创建自定义视图并快速将电话号码传递给它
【发布时间】:2017-02-20 10:57:32
【问题描述】:

我无法在另一个控制器的 for 循环内以编程方式创建视图。父控制器是一个 tableviewcell,我在 CNContact 对象内循环了一堆电话号码。对于联系人拥有的每个电话号码,我希望创建我的自定义视图并将其添加到 tableviewcell 并使其垂直堆叠。

到目前为止,我设法创建了视图并将其添加到 tableviewcell,但无法传递数据。这是我正在努力解决的从一个控制器到另一个控制器的数据传递。

这是我的代码:

ContactListTableViewCell.swift

import UIKit
import Contacts

class ContactListTableViewCell: UITableViewCell {

    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var phonenumberView: UIView!

    func configureCell(contact: CNContact) {
        titleLabel.text = "\(contact.givenName) \(contact.familyName)"

        for phoneNumber in contact.phoneNumbers {

            let view = self.createContactListTableViewTelephoneRow(telephone: phoneNumber)
            self.phonenumberView.addSubview(view)


        }


    }

    func createContactListTableViewTelephoneRow(telephone: Any) -> UIView {
        let controller = ContactListTableViewTelephoneRow()
        let view = UINib(nibName: "ContactListTableViewTelephoneRow", bundle: nil).instantiate(withOwner: controller, options: nil)[0] as! UIView
        return view
    }

}

Main.storyboard 中的contactListTableViewCell 原型

ContactListTableViewTelephoneRow.swift

class ContactListTableViewTelephoneRow: UIView {

    @IBOutlet var view: UIView!
    @IBOutlet weak var telephoneLabel: UILabel!
    @IBOutlet weak var telephoneTypeLabel: UILabel!

    func setData(telephoneLabelText: String, telephoneTypeLabelText: String) {
        telephoneLabel?.text = telephoneLabelText
        telephoneTypeLabel?.text = telephoneTypeLabelText
    }


}

ContactListTableViewTelephoneRow.xib

任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 你能告诉我你是如何发送数据的吗?

标签: ios swift xcode swift3 uinib


【解决方案1】:

传递数据的简单方法是您需要在第二个控制器中创建对象并从第一个控制器传递数据

let vc = self.storyboard!.instantiateViewController(withIdentifier: "Secondcontroller") as! Secondcontroller
    vc.yourObject = object //To pass
    self.present(tabvc, animated: true, completion: nil) // or push

【讨论】:

    【解决方案2】:

    您需要转换使用UNib.[...] 创建的视图并将数据直接传递给它:

    func createContactListTableViewTelephoneRow(telephone: CNLabeledValue<CNPhoneNumber>) -> UIView {
        let nib = UINib(nibName: "ContactListTableViewTelephoneRow", bundle: nil)
        let root = nib.instantiate(withOwner: nil, options: nil)[0]
        let view = root as! ContactListTableViewTelephoneRow
    
        view.setData(telephoneLabelText: telephone.value.stringValue,
                     telephoneTypeLabelText: telephone.label!) // make sure `telephone.label!` is correct – I never compiled it
    
        return view
    }
    

    注意我调整了createContactListTableViewTelephoneRow(telephone:)的签名。

    但作为一个整体建议:我会以非常不同的方式解决您的 UI 问题。

    背景:UITableViews 大量重用(队列/出队)单元格,因此滚动性能可以接受。虽然我假设您使用 UITableViewDataSource 的 API 在您的单元格中正确加载 nib 会很快成为性能瓶颈。

    我建议不要在您的单元格中使用可变数量的ContactListTableViewTelephoneRow。而是让它成为UITableViewCell 的子类。在这种情况下,您的视图控制器当然必须处理至少两种不同类型的单元格。您可以使用不同的部分来保持逻辑相当简单。这是一个完整的示例:(您当然需要调整样式)

    import Contacts
    import UIKit
    
    class ContactListTelephoneTableViewCell: UITableViewCell {
    
        @IBOutlet weak var telephoneLabel: UILabel!
        @IBOutlet weak var telephoneTypeLabel: UILabel!
    
        func configureCell(telephone: CNLabeledValue<CNPhoneNumber>) {
            telephoneLabel.text = telephone.value.stringValue
            telephoneTypeLabel.text = telephone.label!
        }
    }
    
    class ContactListTableViewCell: UITableViewCell {
    
        @IBOutlet weak var titleLabel: UILabel!
    
        func configureCell(contact: CNContact) {
            titleLabel.text = "\(contact.givenName) \(contact.familyName)"
        }
    }
    
    
    class DataSource: NSObject, UITableViewDataSource {
    
        var contacts: [CNContact]!
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return contacts[section].phoneNumbers.count + 1 // one extra for given and family name
        }
    
          func numberOfSections(in tableView: UITableView) -> Int {
            return contacts.count
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            if indexPath.row == 0 {
                return self.tableView(tableView, nameCellForRowAt: indexPath)
            } else {
                return self.tableView(tableView, phoneCellForRowAt: indexPath)
            }
        }
    
        func tableView(_ tableView: UITableView, nameCellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "name", for: indexPath) as! ContactListTableViewCell
            cell.configureCell(contact: contacts[indexPath.section])
            return cell
        }
    
        func tableView(_ tableView: UITableView, phoneCellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "phone", for: indexPath) as! ContactListTelephoneTableViewCell
    
            let contact = contacts[indexPath.section]
            let telephone = contact.phoneNumbers[indexPath.row - 1] // minus one for given and family name
            cell.configureCell(telephone: telephone)
    
            return cell
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-31
      • 2020-07-18
      • 2015-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-25
      相关资源
      最近更新 更多