func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int {
return 5
}
func tableView(tableView:UITableView!, cellForRowAtIndexPath indexPath:NSIndexPath!) -> UITableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier("BookCell") as BookTableViewCell
println("ip: \(indexPath.row)")
cell.bookLabel.text = "Row #\(indexPath.row)"
return cell
}
我只看到一个单元格,文本被覆盖了 5 次,而不是 5 个单元格。我以为我们不必再做 if (!cell) 废话了?我做错了什么?
最佳答案
如果您看到 UILabels 被覆盖了 5 次,那是因为您的单元格高度尚未定义并且计算为 0 像素高。以下任何一项都可以帮助您显式或隐式定义行高:
1) 在 Interface Builder 中设置行高:
2)实现UITableViewDataSource
协议(protocol)方法:
func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
return 50.0
}
3) 在单元格的 UILabel
和 UITableViewCell
的外边界之间设置自动布局约束:
关于ios - Swift 中的 UITableView - 单元格出队,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24836012/