1

我无法在另一个控制器的 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

在此处输入图像描述

任何帮助将非常感激。谢谢你。

4

2 回答 2

0

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

let vc = self.storyboard!.instantiateViewController(withIdentifier: "Secondcontroller") as! Secondcontroller
    vc.yourObject = object //To pass
    self.present(tabvc, animated: true, completion: nil) // or push
于 2017-02-22T10:40:03.363 回答
0

您需要转换使用创建的视图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 大量重用(队列/出队)单元格,因此滚动性能是可以接受的。尽管我假设您使用 APIUITableViewDataSource正确加载单元格内的 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
    }
}
于 2017-02-22T15:50:32.437 回答