繁体   English   中英

自定义 UITableViewCell 中的 Outlets 始终为零

[英]Outlets are always nil in custom UITableViewCell

我有一个自定义的 UITableViewCell,我用界面构建器设计了一个标签和一个选择器。 对于实现 swift 文件,我添加了两个出口来引用标签和选择器。 插座连接到 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
    }

}

我有一个 UITableViewController,我想在这个单元格中使用它。 我将它设置为静态是因为我使用它来显示设置,并且单元格的数量永远不会以编程方式改变。 我将我的一个自定义单元格添加到表格视图并将其标识符设置为 MediaStyleCell。

然后,在 UITableViewController 的 swift 文件中,我将此行添加到我的 viewDidLoad 函数中

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

其中 TitlePickerCell 是 .xib 文件的名称,MediaStyleCell 是我使用界面生成器为单元格提供的标识符。 当我运行我的应用程序时,它崩溃了,因为我的自定义单元格类中的标签和选择器为零。

如果你使用 tableview.registerClass:forCellReuseIdentifier:但你也有单元格的 xib 文件,你会得到这个错误。

所以代替

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

只是使用

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

我认为发生这种情况的唯一原因是,如果插座未正确链接,正在注册不正确的笔尖/标识符或 .xib 文件中的重用标识符丢失/不正确。

无法重现,这里是我使用的 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
    }
}

这是单元格:

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

}

我得到了这个漂亮丑陋的细胞(我只是把它变成了蓝色,因为它是第一次自动完成)

在此处输入图片说明

确保插座已实际连接:

打开 xib 文件,然后在右上角,转到 outlet 菜单。 连接那里的插座。

插座连接

创建自定义 UITableViewCell 似乎是解决此问题的错误方法。 只需将单元格样式设置为自定义并以这种方式添加 UI 元素即可。

暂无
暂无

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

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