在tableView(Swift)中添加填充函数

时间:2017-11-13 09:37:30

标签: ios swift uitableview swift3 uicollectionview

我想调整此功能(最初是为collectionView创建的)

 let place = places[indexPath.row]
        let annotationPadding = CGFloat(5)

        let font = UIFont.systemFont(ofSize: 15)
        let commentHeight = place.heightForComment(font, width: width)

        let height = annotationPadding + commentHeight + annotationPadding

        return height 

所以我可以在func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { }的{​​{1}}中添加它,我该怎么办? (我的问题是tableView不接受CGFloat

1 个答案:

答案 0 :(得分:0)

假设集合视图的heightForComment采用集合视图单元格的宽度,则可以传入表格视图的宽度,因为表格视图单元格与其包含的表格视图一样宽:

let commentHeight = place.heightForComment(font, width: tableView.bounds.size.width)

如果表格视图中有注释的其他前导或尾随边距,则可以从表格视图宽度中减去该边距:

let commentHeight = place.heightForComment(font, width: tableView.bounds.size.width - (20 * 2)) // 20 points margin on left and right