【问题标题】:UITableView in Swift - dequeueing of cellsSwift 中的 UITableView - 单元格出列
【发布时间】:2014-07-19 01:52:35
【问题描述】:
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) 废话了?我做错了什么?

【问题讨论】:

  • 工作正常,就像旧的好“废话”一样。
  • 你搞砸了自动布局吗?

标签: ios uitableview swift xcode6


【解决方案1】:

如果您看到 UILabel 被覆盖 5 次,那是因为您的单元格高度尚未定义,并且它被计算为 0 像素高。以下任何一项都可以帮助您显式或隐式定义行高:

1) 在 Interface Builder 中设置行高:

2)实现UITableViewDataSource协议方法:

func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
    return 50.0
}

3) 在单元格的UILabelUITableViewCell 的外部边界之间设置自动布局约束:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 2021-03-26
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多