简体   繁体   English

自定义 UITableviewCell IBOutlet 始终为零

[英]Custom UITableviewCell IBOutlet always nil

I have a custom UITableViewCell subclass with a simple IBOutlet setup for a UILabel.我有一个自定义 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")
    }

}

Convinced I have everything set up correct have followed other answers, but the UILabel is always nil.确信我已正确设置所有内容并遵循其他答案,但 UILabel 始终为零。

ViewController: viewDidLoad:视图控制器:viewDidLoad:

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

cellForForAtIndexPath cellForForAtIndexPath

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

What am I missing?我错过了什么?

Based on the way you register your cell, it is not going to be loading it from a storyboard or xib file.根据您注册单元格的方式,它不会从故事板或 xib 文件中加载它。 It will be invoking that init method only.它将仅调用该 init 方法。 Your init method does not create the label, so it will always be nil .您的 init 方法不会创建标签,因此它将始终为nil

You should also use dequeueReusableCellWithIdentifier(_:forIndexPath:) instead of dequeueReusableCellWithIdentifier(_:) .您还应该使用dequeueReusableCellWithIdentifier(_:forIndexPath:)而不是dequeueReusableCellWithIdentifier(_:) The latter predates storyboards and will return nil unless you have previously created a cell with that identifier and returned it.后者早于情节提要并且将返回nil除非您之前使用该标识符创建了一个单元格并返回它。

Lastly, the init method that the tableView is calling is not the one you've implemented, or the app would crash on test.text = ... while trying to unwrap a nil optional.最后, tableView调用的 init 方法不是您已实现的方法,否则应用程序会在test.text = ...尝试解开可选的nil时崩溃。

Since You are uisg Xib file you have to register with ,由于您是 uisg Xib 文件,因此您必须注册,

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

Think, if only register with class ,system will not know the its Xib.This work for me.想一想,如果只注册类,系统就不会知道它的 Xib。这对我有用。

UITableViewCell 类和 ContentView 类不能相同。

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

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

I have same issue, after add custom cell class to cell in tableview placed in Storyboard.我有同样的问题,在将自定义单元格类添加到放置在 Storyboard 中的 tableview 中的单元格之后。 I also register cell, like in documentation.我还注册了单元格,就像在文档中一样。 But now I solve my issue.但现在我解决了我的问题。

"I remove registering and inspect cell again, all IBOutlets initialised" “我删除注册并再次检查单元格,所有 IBOutlets 都已初始化”

I think that basically the outlets are not setup yet at init time.我认为基本上插座在初始化时还没有设置。 If you want to get to the outlets and manipulate them, then override didMoveToSuperview or similar and do it in there.如果你想到达出口并操纵它们,那么覆盖 didMoveToSuperview 或类似的东西并在那里做。 For instance, this is what I had to do to get to the button outlet in my cell:例如,这是我必须做的才能到达我的单元格中的按钮出口:

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)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM