自定义表格视图单元格:IBOutlet标签为零

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

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

此代码导致以下错误。

致命错误:意外地发现零,而解包一个可选值

titleLabel是零 – 这不清楚为什么。 设置UITableViewCell默认属性(如textLabel )工作得很好。

我正在使用自定义单元格的笔尖。

标签和表格视图都正确连接到他们的IBOutlet

标签桌子视图

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

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

示例XCode项目可用 。

首先,您正在使用一个nib文件将您的自定义单元格加载到表格中。 如果你是Swift / Cocoa的新手,那么这可能会更令人头疼。 我会暂时把一切都转移到故事板上。 而不是使用nib文件单击,转到故事板,单击您的UITableView ,并确保TableView的内容设置是Dyanamic Prototypes

在这里输入图像说明

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

在这里输入图像说明在这里输入图像说明

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

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

它应该运行。

尝试这个

 import UIKit class ViewController: UIViewController, UITableViewDataSource { @IBOutlet weak var tableView: UITableView! var items = ["One", "Two", "Three"] override func viewDidLoad() { super.viewDidLoad() self.tableView.registerNib(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "customCell")// 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", forIndexPath: indexPath) as CustomTableViewCell cell.titleLabel!.text = self.items[indexPath.row] return cell } } 

您应该使用dequeueReusableCellWithIdentifier:forIndexPath来使单元出队。

如果您正在使用故事板创build单元格,则不需要注册该类以供重用,因为如果您设置了reuseIdentifier,故事板会为您重新使用。

请确保您在viewdidload中注册您的笔尖(自定义单元格)时没有任何错误。 笔尖名称和重用标识符不应该是错的。

像这样注册你的笔尖:

 let bundle = Bundle(for: type(of: self)) let nib = UINib(nibName: "PickerCell", bundle: bundle) collectionViewPicker.register(nib, forCellWithReuseIdentifier: "cell") 

你的CustomTableViewCell中的出口必须有强大的参考。

更换

 @IBOutlet weak var titleLabel: UILabel! 

 @IBOutlet var titleLabel: UILabel! 

为我工作没有!

编辑

  let challenge = self.challenges![indexPath.row] let title = challenge["name"] if title != nil { cell.titleLabel.text = title }