Swift中的UITableView - 单元格出列

时间:2014-07-19 01:52:36

标签: ios uitableview swift xcode6

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)废话了吗?我做错了什么?

1 个答案:

答案 0 :(得分:0)

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

1)在Interface Builder中设置行高:

enter image description here

2)实施UITableViewDataSource协议方法:

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

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

enter image description here