gpt4 book ai didi

ios - 自定义表格单元格类在 cellForRowAt 中不起作用

转载 作者:行者123 更新时间:2023-11-28 15:17:29 26 4
gpt4 key购买 nike

这个问题让我疯狂了 3 天。我创建了一个自定义单元类:

class TransitoPalinaTableViewCell: UITableViewCell {

@IBOutlet weak var lblOrario: UILabel!
@IBOutlet weak var lblDestinazione: UILabel!
@IBOutlet weak var lblNote: UILabel!
@IBOutlet weak var lblBus: 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
}

}

在我的 View Controller 中我有这个:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TransitoPalinaTableViewCell

// Configure the cell...
let hour = Int(self.transito.listaCorse[indexPath.row].tempoTransito) / 3600
let mins = Int(self.transito.listaCorse[indexPath.row].tempoTransito % 3600) / 60

cell.lblOrario?.text = String(format: "%02d:%02d", hour, mins)
cell.lblDestinazione?.text = self.transito.listaCorse[indexPath.row].arrivoCorsa
cell.lblNote?.text = ""
cell.lblBus?.text = self.transito.listaCorse[indexPath.row].automezzo


cell.backgroundColor = UIColor.init(red: 0, green: 60/255, blue: 113/255, alpha: 1)

return cell
}

在 Storyboard 中,我设置了 TableView 单元格标识符:“Cell”和自定义类“TransitoPalinaTableViewCell”。

问题是当调用 cellForRowAt 时出现错误

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

在 dequeueReusableCell 命令的行。

正如我在其他帖子中所读到的,我没有放这条线self.tableView.register(TransitoPalinaTableViewCell.self, forCellReuseIdentifier: "Cell") 在 vi​​ewdidLoad() 中 - 它会创建一个全新的单元模型,不与 Storyboard 中的 socket 连接。我尝试再次创建 TransitoPalinaTableViewCell 类及其导出,但没有任何改变。

感谢任何帮助。

最佳答案

您可以使用单元格类或单元格 Nib 注册单元格。

tableView.register(UINib(name: "YourNibName", bundle: nil), forCellReuseIdentifier: "Cell")

但这只有在您在 Xib 文件中创建单元格时才有效。如果它是在 Storyboard 中创建的,那么除了您在其中创建它的 ViewController 之外,恐怕无法在其他任何地方使用该单元格。

关于ios - 自定义表格单元格类在 cellForRowAt 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46587433/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com