自定义表格视图单元格:IBOutlet标签为零 [英] Custom table view cell: IBOutlet label is nil

查看:127
本文介绍了自定义表格视图单元格:IBOutlet标签为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下简单视图控制器:

Consider the following simple view controller:

class ViewController: UIViewController, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!

    var items = ["One", "Two", "Three"]

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "customCell")
        self.tableView.dataSource = self
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.items.count;
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCellWithIdentifier("customCell") as CustomTableViewCell
        cell.titleLabel!.text = self.items[indexPath.row]
        return cell
    }
}

自定义单元格视图:

class CustomTableViewCell: UITableViewCell {
    @IBOutlet weak var titleLabel: UILabel!   
}

此代码导致以下错误。


致命错误:在解包可选值时意外发现nil

fatal error: unexpectedly found nil while unwrapping an Optional value

titleLabel 是零 - 目前尚不清楚原因。设置 UITableViewCell 的默认属性(如 textLabel )工作正常。

titleLabel is nil — it's not clear why. Setting default properties of UITableViewCell (like textLabel) work just fine.

我正在为自定义单元格使用nib。

I'm using a nib for the custom cell.

标签和表格视图都正确连接到 IBOutlet s。

Both the labels and table views are correctly connected to their IBOutlets.


原型单元格并且自定义nib视图被标记为具有 CustomTableViewCell 类。

Both the prototype cell and the custom nib view are marked as having a CustomTableViewCell class.

我是iOS开发的新手,上午我错过了一些明显的东西?

I'm new to iOS development, am I missing something obvious?

示例XCode项目可用

推荐答案

首先,您正在使用nib文件加载自定义单元格进入表格。如果你是Swift / Cocoa的新手,那可能会比它更值得头疼。我暂时将所有内容都移到故事板上。而不是使用nib文件单击,转到Storyboard,单击 UITableView 并确保TableView的内容设置为 Dyanamic Prototypes

First off, you're using a nib file to load your custom cell into the table. That's probably going to be more of a headache than it's worth if you're new to Swift/Cocoa. I would move everything over to storyboard for the time being. Instead of using a nib file click, go to Storyboard, click on your UITableView and make sure the TableView's content setting is Dyanamic Prototypes:

接下来,单击原型单元格(表格视图中唯一的单元格)并将类设置为 CustomTableViewCell 并设置其重用标识符为 customCell

Next, click on the prototype cell (the only one in the table view) and set the class to CustomTableViewCell and set its reuse identifier to customCell:


接下来,在原型单元格中添加一个标签,并将其链接到 CustomTableViewCell 类中的IBOutlet。只要在storyboard中设置了重用标识符,就不需要注册 customCell 。删除这一行:

Next, add a label to your prototype cell and link it to the IBOutlet in your CustomTableViewCell class. You don't need to register your customCell so long as you've set the reuse identifier in storyboard. Delete this line:

self.tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "customCell")

它应该运行。

这篇关于自定义表格视图单元格:IBOutlet标签为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆