我想在UIViewController中设置一个UITableView。UITableView有UITableViewCells,它有一个UILabel。
但是,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]
}
}发布于 2019-08-29 05:29:28
删除行
self.condimentTable.register(condimentTableCell.self, forCellReuseIdentifier: "condimentCell")您正在断开与单元格的情节提要连接。
https://stackoverflow.com/questions/57699555
复制相似问题