简体   繁体   English

自定义 UITableViewCell 中的 Outlets 始终为零

[英]Outlets are always nil in custom UITableViewCell

I have a custom UITableViewCell that I designed with interface builder to have a label and a picker.我有一个自定义的 UITableViewCell,我用界面构建器设计了一个标签和一个选择器。 To the implementation swift file, I've added two outlets to refer to the label and picker.对于实现 swift 文件,我添加了两个出口来引用标签和选择器。 The outlets are hooked up to the ui elements and the code looks like this插座连接到 ui 元素,代码如下所示

class TitlePickerCell: UITableViewCell
{

    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var picker: UIPickerView!

    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
    }

}

I have a UITableViewController that I want to use this cell with.我有一个 UITableViewController,我想在这个单元格中使用它。 I set it to static because I'm using it to display settings and the number of cells will never change programmatically.我将它设置为静态是因为我使用它来显示设置,并且单元格的数量永远不会以编程方式改变。 I add one of my custom cells to the table view and set its identifier to MediaStyleCell.我将我的一个自定义单元格添加到表格视图并将其标识符设置为 MediaStyleCell。

Then, in my swift file for the UITableViewController, I add this line to my viewDidLoad function然后,在 UITableViewController 的 swift 文件中,我将此行添加到我的 viewDidLoad 函数中

self.settingsTable.registerNib(UINib.init(nibName: "TitlePickerCell", bundle: nil), forCellReuseIdentifier: "MediaStyleCell")

where TitlePickerCell is the name of the .xib file and MediaStyleCell is the identifier that I gave the cell with interface builder.其中 TitlePickerCell 是 .xib 文件的名称,MediaStyleCell 是我使用界面生成器为单元格提供的标识符。 When I run my app, it crashes because the label and picker in my custom cell class are nil.当我运行我的应用程序时,它崩溃了,因为我的自定义单元格类中的标签和选择器为零。

If you are using tableview.registerClass:forCellReuseIdentifier: but you have cell's xib file also, you will get this error.如果你使用 tableview.registerClass:forCellReuseIdentifier:但你也有单元格的 xib 文件,你会得到这个错误。

So instead of所以代替

tablView.register(Cell.self, forCellReuseIdentifier: cellToDisplay.identifier)

just use只是使用

tableView.register(UINib(nibName: cellToDisplay.identifier, bundle: nil), forCellReuseIdentifier: cellToDisplay.identifier)

Only reason I can think for this to happen is if the outlets are not linked correctly, the incorrect nib/identifier is being registered or the reuse identifier in the .xib file is missing/incorrect.我认为发生这种情况的唯一原因是,如果插座未正确链接,正在注册不正确的笔尖/标识符或 .xib 文件中的重用标识符丢失/不正确。

Unable to reproduce, here is VC code I used:无法重现,这里是我使用的 VC 代码:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.registerNib(UINib.init(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "DifferentIdentifier")
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 44
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell: CustomTableViewCell = tableView.dequeueReusableCellWithIdentifier("DifferentIdentifier", forIndexPath: indexPath) as! CustomTableViewCell
        cell.titleLabel.text = "HERPITY"
        return cell
    }
}

here is cell:这是单元格:

class CustomTableViewCell: UITableViewCell {

    @IBOutlet var titleLabel: UILabel!

    @IBOutlet var pickerView: UIPickerView!

    override func awakeFromNib() {
        super.awakeFromNib()
        titleLabel.text = "Works"
        pickerView.backgroundColor = UIColor.blueColor()
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

}

and I get this nice ugly cell (I just made it blue cos it was first auto-complete)我得到了这个漂亮丑陋的细胞(我只是把它变成了蓝色,因为它是第一次自动完成)

在此处输入图片说明

Make sure the outlets are actually connected:确保插座已实际连接:

Open the xib file and on the top right side, go to the outlets menu.打开 xib 文件,然后在右上角,转到 outlet 菜单。 Connect the outlets in there.连接那里的插座。

插座连接

It seems that creating a custom UITableViewCell is the wrong way to go about this.创建自定义 UITableViewCell 似乎是解决此问题的错误方法。 Just setting the cell style to custom and adding UI elements that way is what should be done.只需将单元格样式设置为自定义并以这种方式添加 UI 元素即可。

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

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