自定义表格视图单元格: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 }
- 如何从“发件人”对象访问用户定义的运行时属性?
- 在UIView里面的空白空间UITableView
- iOS UILabel autoshrink,所以单词不会被截断为两行
- 如何在iOS中的Google地图上添加button?
- 设置TeamCity集成到OSX Build Agent
- 构build设置:我应该在构build设置中做什么更改,以便在发布应用程序时,它将在App Store上显示为仅英语的语言?
- ios创build,然后dynamic添加UI元素
- Xcode故事板:为什么iPad故事板显示iPhone大小的视图?
- 为什么我的collections查看标题“闪光”白色,当我插入一个项目的部分? 任何方式来防止这种情况?