我想创建一个自定义表格 View 。我有一个 UITableViewController:
class MainListView: UITableViewController{
var invoceList: [InvoceObject] = []
@IBOutlet var tbMain: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(CustomCell.self, forCellReuseIdentifier: "cell")
tbMain.delegate = self
self.tbMain.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.invoceList.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
cell.lblBuyerName?.text = self.invoceList[indexPath.row].BUYER_NAME
cell.lblDate?.text = self.invoceList[indexPath.row].CREATED_AT
cell.lblPriceGross?.text = self.invoceList[indexPath.row].PRICE + " " + self.invoceList[indexPath.row].CURRENCY
cell.backgroundColor = UIColor(white: 1, alpha: 0.5)
return cell
}
}
还有我的 UITableViewCell 类:
class CustomCell: UITableViewCell {
@IBOutlet weak var lblBuyerName: UILabel!
@IBOutlet weak var lblDate: UILabel!
@IBOutlet weak var lblPriceGross: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
构建后,我得到一个空单元格。我有许多行,例如 invoceList.count,但没有标签,它看起来像空表。我做错了什么?
最佳答案
由于您使用的是 UITableViewController
有一个隐含的 tableView
具有连接的数据源和委托(delegate)的属性。您应该使用它来避免混淆。
并删除 self.tableView.register...
行如果您使用的是原型(prototype)单元。
关于ios - 自定义单元格内容不显示 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41700056/