[英]UILabel in UITableViewCell returns nil
我想在UIViewController中設置一個UITableView。 UITableView具有包含UILabel的UITableViewCells。
但是,UITableView.UILabels返回nil。
請查看以下代碼和圖像並分享您的想法。
先感謝您。
我正在用Swift5開發它。
import UIKit
class condimentTableCell: UITableViewCell {
@IBOutlet weak var condimentName: UILabel!
}
class SandwichViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var condimentTable: UITableView!
@IBOutlet weak var breadSelector: UIPickerView!
fileprivate let items:[product] = products().getData()
fileprivate let cart:cart = Registry.instance.liveCart
var itemNo: Int = 0
let condiments = ["Lettuce", "Mayo", "Mustard", "Purple Onions", "Tomato"]
let breads = ["Rye", "Sourdough", "Squaw", "Wheat", "White"]
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background.png")!)
self.breadSelector.dataSource = self
self.breadSelector.delegate = self
self.condimentTable.register(condimentTableCell.self, forCellReuseIdentifier: "condimentCell")
self.condimentTable.delegate = self
self.condimentTable.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return condiments.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:condimentTableCell = tableView.dequeueReusableCell(withIdentifier: "condimentCell", for: indexPath) as! condimentTableCell
if (cell.condimentName == nil) {
print("nil")
} else {
cell.condimentName.text = condiments[indexPath.row]
}
print("----------------------------------------------")
return cell
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return breads.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return breads[row]
}
}
刪除線
self.condimentTable.register(condimentTableCell.self, forCellReuseIdentifier: "condimentCell")
您正在斷開情節提要與單元的連接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.