ios – 我们可以将数据从表格单元格传递到表格视图,其中两个都是
发布时间:2020-12-14 19:05:32 所属栏目:百科 来源:网络整理
导读:我想将表格单元格数据(xib文件)传递给表格视图(也是一个xib文件).我尝试使用以下代码传递数据,但没有得到适当的结果. PropertyCell.swift import UIKit class PropertyCell: UITableViewCell { @IBOutlet weak var propertyCodeLbl: UILabel! @IBOutlet weak
我想将表格单元格数据(xib文件)传递给表格视图(也是一个xib文件).我尝试使用以下代码传递数据,但没有得到适当的结果.
PropertyCell.swift import UIKit class PropertyCell: UITableViewCell { @IBOutlet weak var propertyCodeLbl: UILabel! @IBOutlet weak var addressLbl: UILabel! } 我在下面附上了PropertyCell的截图 – PropertyCell.xib PropertyCell.xib file PropertyTableVC.swift import UIKit import Alamofire class PropertyTableVC: UITableViewController { @IBOutlet var propertyTabel: UITableView! let URL_Landlord_Property_List = "http://127.0.0.1/source/api/LandlordPropertyList.php" var count: Int = 0 var landlordPropertyArray: [PropertyList]? = [] override func viewDidLoad() { super.viewDidLoad() fetchData() propertyTabel.dataSource = self propertyTabel.delegate = self let nibName = UINib(nibName: "PropertyCell",bundle:nil) self.propertyTabel.register(nibName,forCellReuseIdentifier: "Cell") } func fetchData(){ let urlRequest = URLRequest(url: URL(string: URL_Landlord_Property_List)!) let task = URLSession.shared.dataTask(with: urlRequest) { (data,response,error) in if error != nil{ print(error!) return } print(data!) self.landlordPropertyArray = [PropertyList]() self.count = (self.landlordPropertyArray?.count)! do{ let json = try JSONSerialization.jsonObject(with: data!,options: .mutableContainers) as! [String: AnyObject] if let datafromjson = json["landlords_property_list"] as? [[String: AnyObject]] { print(datafromjson) for data in datafromjson{ var property = PropertyList() if let id = data["ID"] as? Int,let code = data["Code"] as? String,let address1 = data["Address"] as? String { property.id = id property.code = code property.address1 = address1 } self.landlordPropertyArray?.append(property) } print(self.landlordPropertyArray) } DispatchQueue.main.async { self.propertyTabel.reloadData() } }catch let error { print(error) } } task.resume() } override func numberOfSections(in tableView: UITableView) -> Int { return 1 } override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int { return (landlordPropertyArray?.count)! } override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell { // Configure the cell... let cell = tableView.dequeueReusableCell(withIdentifier: "Cell",for: indexPath) as! PropertyCell cell.propertyCodeLbl.text = self.landlordPropertyArray?[indexPath.item].code cell.addressLbl.text = self.landlordPropertyArray?[indexPath.item].address1 return cell } } 附上以下属性表的屏幕截图 – PropertyTableVC.xib PropertyTableVC.xib file 解决方法
你的TableViewCell:
import UIKit protocol yourProtocolName { //add protocol here func getDataFromTableViewCellToViewController (sender : self) //* as you need to pass the table view cell,so pass it as self } class PropertyCell: UITableViewCell { @IBOutlet weak var propertyCodeLbl: UILabel! @IBOutlet weak var addressLbl: UILabel! var delegate : yourProtocolName? //set a delegate override func awakeFromNib() { super.awakeFromNib() if delegate != nil { delegate.getDataFromTableViewCellToViewController(sender :self) * } } } 而你的ViewController: import UIKit import Alamofire class PropertyTableVC: UITableViewController,yourProtocolName { //conform to the protocol you created in tableViewCell @IBOutlet var propertyTabel: UITableView! let URL_Landlord_Property_List = "http://127.0.0.1/source/api/LandlordPropertyList.php" var count: Int = 0 var landlordPropertyArray: [PropertyList]? = [] override func viewDidLoad() { super.viewDidLoad() fetchData() propertyTabel.dataSource = self propertyTabel.delegate = self let nibName = UINib(nibName: "PropertyCell",for: indexPath) as! PropertyCell cell.propertyCodeLbl.text = self.landlordPropertyArray?[indexPath.item].code cell.addressLbl.text = self.landlordPropertyArray?[indexPath.item].address1 return cell } func getDataFromTableViewCellToViewController(sender :UITableViewCell) { //make a callback here } } (*)标记的字段是更新的代码 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |