对于静态单元格

时间:2017-06-30 10:35:43

标签: ios swift uitableview autolayout

对静态单元格使用UITableViewAutomaticDimension后(这是'正确的细节单元格,所以我无法对单元格的高度设置任何约束)

细胞的高度变得很小。有没有什么办法解决这一问题? :(

提前致谢!

What I was expecting(image)

How it shows(image)

代码:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 65
}

override func viewWillAppear(_ animated: Bool) {
    tableView.estimatedRowHeight = 65
    tableView.rowHeight = UITableViewAutomaticDimension
}

编辑:

使用constraint时似乎没有办法在没有UITableViewAutomaticDimension的情况下延长身高,所以我手动设置了rowHeight

struct DeviceSize {
    static let iPhoneSE = (width:Float(320.0), height:Float(568.0))
    static let iPhone7 = (width: Float(375.0), height:Float(667.0))
    static let iPhone7P = (width: Float(414.0), height:Float(736.0))
}

override func tableView(_ tableView: UITableView,
               heightForRowAt indexPath: IndexPath) -> CGFloat {
    let height = Float(UIScreen.main.bounds.size.height)
    switch height {
    case DeviceSize.iPhoneSE.height:
        return 55
    case DeviceSize.iPhone7.height:
        return 60
    case DeviceSize.iPhone7P.height:
        return 65
    default:
        return 65
    }
}

2 个答案:

答案 0 :(得分:2)

在单元格内部,将标签的高度约束Greater than or Equal设置为所需的最小高度。 AutomaticDimensions根据标签的文本内容制作单元格高度。只需为标签指定最小高度,例如this

答案 1 :(得分:1)

如果使用自动尺寸,则将自动计算单元格高度。手动设置行高。

这些行不是必需的

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 65
}
override func viewWillAppear(_ animated: Bool) {
    tableView.estimatedRowHeight = 65
   tableView.rowHeight = UITableViewAutomaticDimension
}

解决方案:

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    if indexPath.row == 0{
    //.... return height whatever you want for indexPath.row
    return 40
    }else {
    return 30
    }
}