【问题标题】:Custom UITableviewCell IBOutlet always nil自定义 UITableviewCell IBOutlet 始终为零
【发布时间】:2016-01-22 03:20:36
【问题描述】:

我有一个自定义 UITableViewCell 子类,其中包含用于 UILabel 的简单 IBOutlet 设置。

class SegmentCell: UITableViewCell {

    @IBOutlet weak var test: UILabel!

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        test.text = "Some Text"
    }

    required init?(coder aDecoder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
    }

}

确信我已经按照其他答案正确设置了所有内容,但 UILabel 始终为零。

视图控制器: viewDidLoad:

self.tableView.registerClass(SegmentCell.self, forCellReuseIdentifier: "Cell")

cellForForAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! SegmentCell
    return cell
}
  • 单元格设置为自定义
  • 重用标识符正确
  • Cells 类是 SegmentCell
  • Tableview 内容是动态原型

我错过了什么?

【问题讨论】:

  • 不需要在 viewDidLoad 上调用 self.tableView.registerClass(SegmentCell.self, forCellReuseIdentifier: "Cell"),只需在 Mainstoryboard 上将prototypeCells 类设置为“SegmentCell”即可。
  • 我意识到这是一个非常古老的问题,但它目前没有答案,我碰巧遇到了它,因为我遇到了类似的问题。在您的情况下,我认为 weak var 是关键 - 如果您的 SegmentCell 是唯一拥有对 UILabel 的引用的东西,那将不会影响保留计数,因此标签将被释放因为没有人强烈提及它。当您尝试设置 .text 时,它已经消失了。

标签: ios swift uitableview storyboard


【解决方案1】:

由于你是uisg Xib文件,你必须注册,

tableView.register(UINib(nibName: "yourNib", bundle: nil), forCellReuseIdentifier: "CellFromNib")

想一想,如果只注册类,系统不会知道它的 Xib。这对我有用。

【讨论】:

    【解决方案2】:

    根据您注册单元格的方式,它不会从情节提要或 xib 文件中加载它。它将仅调用该 init 方法。您的 init 方法不会创建标签,因此它将始终为 nil

    您还应该使用dequeueReusableCellWithIdentifier(_:forIndexPath:) 而不是dequeueReusableCellWithIdentifier(_:)。后者早于情节提要,并且将返回 nil,除非您之前使用该标识符创建了一个单元格并将其返回。

    最后,tableView 调用的 init 方法不是您已实现的方法,否则应用程序会在尝试解开 nil 可选时在 test.text = ... 上崩溃。

    【讨论】:

    • 我在故事板的原型单元格中创建它。
    • 那么你不应该在代码中用tableView注册它,你可以期待init(coder:)被调用。
    • 所以我删除了 tableView.registerClass 并将 SegmentCell 类中的 init 方法更改为 required init?(coder aDecoder: NSCoder) { 仍然有同样的问题
    • 您还应该使用dequeueReusableCellWithIdentifier(_:forIndexPath:) 而不是dequeueReusableCellWithIdentifier(_:)。后者早于情节提要,并将返回nil,除非您之前使用该标识符创建了一个单元格并将其返回。
    • 你不需要一个。您可以在情节提要中设置该文本。
    【解决方案3】:

    UITableViewCell 类和 ContentView 类不能相同。

    【讨论】:

      【解决方案4】:

      你应该先在 viewDidLoad 函数中注册它。

      self.tableView.registerClass(CustomCell.self, forCellReuseIdentifier: "Cell")
      

      【讨论】:

      • 这其实是在正确的轨道上。但调用是这样的:TableView.register(UINib.init(nibName: "CellXibName", bundle: nil), forCellReuseIdentifier: "CellIdentifier")
      【解决方案5】:

      在将自定义单元格类添加到故事板中的表格视图中的单元格后,我遇到了同样的问题。 我也注册单元格,就像在文档中一样。但现在我解决了我的问题。

      “我删除注册并再次检查单元格,所有 IBOutlets 都已初始化”

      【讨论】:

        【解决方案6】:

        我认为基本上在初始化时还没有设置出口。如果您想到达网点并对其进行操作,请覆盖 didMoveToSuperview 或类似方法并在其中进行操作。例如,我必须这样做才能到达单元格中的按钮插座:

        open class MyTableViewCell: UITableViewCell {
            // Top stack view, not the inner one.
            @IBOutlet weak var stackView: UIStackView!
            @IBOutlet weak var buttonView: UIButton?
        
            open override func didMoveToSuperview() {
                buttonView?.titleLabel?.adjustsFontForContentSizeCategory = true
            }
        
            func adjustForAccessibilityCategory(accessible: Bool) {
                if accessible {
                    stackView.axis = .vertical
                    stackView.alignment = .leading
                    stackView.spacing = 2
                } else {
                    stackView.axis = .horizontal
                    stackView.alignment = .center
                    stackView.spacing = 20
                }
            }
        
            open override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
                if #available(iOS 11.0, *) {
                    adjustForAccessibilityCategory(accessible: traitCollection.preferredContentSizeCategory.isAccessibilityCategory)
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-12-06
          • 1970-01-01
          • 1970-01-01
          • 2012-12-14
          相关资源
          最近更新 更多