考虑下面的简单视图控制器:
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
titleLabel
是零——不清楚为什么.将默认属性设置为UITableViewCell
(比如textLabel
)效果很好.
我用笔尖做定制电池.
标签和表格视图都已正确连接到它们的IBOutlet
.
原型单元和定制nib视图都被标记为具有CustomTableViewCell
类.
我是iOS开发新手,我是不是错过了一些明显的东西?