考虑以下简单视图控制器:
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
}
}
Run Code Online (Sandbox Code Playgroud)
和自定义单元格视图:
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var titleLabel: UILabel!
}
Run Code Online (Sandbox Code Playgroud)
此代码导致以下错误.
致命错误:在展开Optional值时意外发现nil
titleLabel是零 - 不清楚为什么.设置 …
我的集合视图中的每个部分都需要一个标签.我试过简单地将标签拖到原型单元格上方的标题空间中,但每次运行应用程序时,标签都不可见.
有帮助吗?