【问题标题】:Add a padding function in a tableView (Swift)在 tableView (Swift) 中添加填充函数
【发布时间】:2017-11-13 09:37:29
【问题描述】:

我想调整这个功能(最初是为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 

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

【问题讨论】:

  • 将宽度创建为局部变量,例如 var width: CGFloat = 0 并在您有宽度时设置它。

标签: ios swift uitableview swift3 uicollectionview


【解决方案1】:

假设集合视图的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

【讨论】:

    猜你喜欢
    • 2016-09-14
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-06
    相关资源
    最近更新 更多