jjzjj

ios - dequeueReusableCellWithIdentifier 不返回单元格

coder 2023-09-07 原文

我有一个包含一个静态部分的静态表。其他部分是动态的。

我为动态部分创建表格部分和表格单元格。为 Cell 设置标识符,为其设置自定义类,甚至可以执行以下操作:

self.tableView.registerClass(UncheckedStoreTableViewCell.self, forCellReuseIdentifier: "StoreCell")

如果我不使用代码注册它,那么我会得到:

'unable to dequeue a cell with identifier StoreCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

所以当我使用它时:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if indexPath.section == 0 {
        return super.tableView(tableView, cellForRowAtIndexPath: indexPath)
    }

    let cell = tableView.dequeueReusableCellWithIdentifier("StoreCell", forIndexPath: indexPath) as! UncheckedStoreTableViewCell

    return cell
}

它有效。但是如果我想改变标签:cell.myLabel.text = "one" 或者只是 print(cell.myLabel) 得到

BAD_INSTRUCTION

最佳答案

  1. 您绝对可以在静态表格 View 中使用动态单元格。
  2. 不要期望静态 TableView 会为您注册单元格的标识符。自己动手吧。
  3. 您是否在单元格类中有导出到界面生成器中的某些 View ?如果我是你,我不会期望 TableView 知道这一点。它将实例化您的单元类,仅此而已。不会设置网点。我认为这是相关的:load nib in view subclass

顺便说一句,如果您已经为您的单元格定义了一个自定义的 .nib,则有这个方法:registerNib(_:forCellReuseIdentifier:)

关于ios - dequeueReusableCellWithIdentifier 不返回单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35607397/

有关ios - dequeueReusableCellWithIdentifier 不返回单元格的更多相关文章

随机推荐