2017-02-20 51 views
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 
    } 

} 

contactListTableViewCell原型内Main.storyboard enter image description here

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 
    } 


} 

CONTA ctListTableViewTelephoneRow.xib

enter image description here

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

+0

你能告诉我你是如何发送数据。 – Prabhat

回答

0

传递数据的简单方法是,你需要箱子对象在你的第二个控制器和第一控制器通过数据

let vc = self.storyboard!.instantiateViewController(withIdentifier: "Secondcontroller") as! Secondcontroller 
    vc.yourObject = object //To pass 
    self.present(tabvc, animated: true, completion: nil) // or push 
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:)的签名。

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

背景:UITableView严重重复使用(队列/出队)单元,以便滚动性能可以接受。尽管我假设您使用UITableViewDataSource的API正确加载您的单元格内的笔尖可能会非常快地成为性能瓶颈。

我建议不要在您的手机中设置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 
    } 
}