【问题标题】:Custom table cell class not working in cellForRowAt自定义表格单元格类在 cellForRowAt 中不起作用
【发布时间】:2017-10-05 22:50:24
【问题描述】:

这个问题让我发疯了 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
}

}

在我的视图控制器中我有这个:

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
}

在情节提要中,我设置了表格视图单元格标识符:“Cell”和自定义类“TransitoPalinaTableViewCell”。

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

由于未捕获的异常而终止应用程序 'NSInternalInconsistencyException',原因:'无法使单元格出列 带有标识符 Cell - 必须为 标识符或连接故事板中的原型单元'

在 dequeueReusableCell 命令的行。

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

任何帮助表示赞赏。

【问题讨论】:

  • 可能显然是的,但只是为了排除原因......在您的故事板中,您是否将您的单元格设计为表格视图中的原型单元格?
  • 这个链接很有帮助stackoverflow.com/questions/540345/…
  • @DonMag 是的,我做到了。在我的故事板中,我将对象放在单元格中,并在表格中设置了 1 个动态原型单元格。
  • @cicaletto79 - 再次,只是为了排除可能的原因......您将“单元格”(无引号,区分大小写)放在属性检查器下的标识符字段中,如下所示:imgur.com/A0LjMjz
  • @cicaletto79 你在故事板中给出了正确的类名和单元格标识符吗??

标签: ios swift identifier tablecell


【解决方案1】:

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

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

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

【讨论】:

  • 我尝试了您的建议,将 let cellNib = UINib.init(nibName: "Cell", bundle: nil) self.tableView.register(cellNib, forCellReuseIdentifier: "Cell") 放入我的 viewDidLoad 但我收到错误“无法在包中加载 NIB”
  • 就像我写的那样 - 如果单元格是在 Xib 中创建的,而不是在 Storyboard 中创建的,那么它会起作用。您的单元格是在 Xib 中还是在 Storyboard 中?
【解决方案2】:

我使用 .xib 文件创建了一个新的 CustomTableViewCell 类,并使用该文件将插座与该类连接起来。我在 xib 文件的属性检查器中将 customCell 设置为标识符,并从表视图控制器的情节提要中删除了任何其他标识符。

在 viewDidLoad 我这样注册了 Cell:

let cellNib = UINib.init(nibName: "CustomTableViewCell", bundle: nil)
self.tableView.register(cellNib, forCellReuseIdentifier: "customCell")

在cellForRowAt中我使用了

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    相关资源
    最近更新 更多