对静态单元格使用UITableViewAutomaticDimension
后(这是'正确的细节单元格,所以我无法对单元格的高度设置任何约束)
提前致谢!
代码:
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
}
}
答案 0 :(得分:2)
答案 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
}
}