Ios 表格视图显示正确的单元格´;滚动一段时间后的高度是多少
我对表格vie单元格有一些问题,只是在滚动了一些单元格之后,单元格才获得了正确的高度。 . 添加了layoutIfNeeded(),但我似乎不工作 我的代码:Ios 表格视图显示正确的单元格´;滚动一段时间后的高度是多少,ios,swift,xcode,uitableview,Ios,Swift,Xcode,Uitableview,我对表格vie单元格有一些问题,只是在滚动了一些单元格之后,单元格才获得了正确的高度。 . 添加了layoutIfNeeded(),但我似乎不工作 我的代码: func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if tableView === MainTable{ if let cell = tableVi
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView === MainTable{
if let cell = tableView.dequeueReusableCell(withIdentifier: "MedName", for: indexPath) as? MedCell{
let backgroundView = UIView()
backgroundView.backgroundColor = UIColor.white.withAlphaComponent(0.0)
cell.selectedBackgroundView = backgroundView
cell.setMedName(name: self.medCatalog[indexPath.row].nombre, uso: self.medCatalog[indexPath.row].uso )
cell.layoutIfNeeded()
cell.updateConstraintsIfNeeded()
cell.layoutSubviews()
self.cellBool[indexPath.row] = true
self.collapsedCellHeight[indexPath.row] = cell.medText.bounds.height
return cell
}
}
self.cellBool[indexPath.row] = false
self.collapsedCellHeight[indexPath.row] = 0.0
self.expandedCellHeight[indexPath.row] = 0.0
return UITableViewCell()
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if self.cellBool[indexPath.row] != nil {
if self.cellBool[indexPath.row]!{
let nameHeight = self.collapsedCellHeight[indexPath.row]!
let topAnchor: CGFloat = 12.0
let bottomAnchor: CGFloat = 10.0
let cellHeight = nameHeight + topAnchor + bottomAnchor + 9 + 2
return cellHeight
}
}
return 85
}
编辑
我忘了提到主要目标是创建一个可扩展和可折叠的行,这就是为什么在蓝色UIlabel下面有一个UIlabel。
蓝色uilabel文本来自json。json中的一些值有点长,因此uilabel会创建另一行来显示所有内容。这就是我需要提供单元格高度的原因,因为有些标签有一行,而其他标签有多行,我需要计算蓝色标签的高度,然后在
didSelectRowAtIndexPath
上展开整个单元格。我知道如何展开和折叠数据行,但我不知道为什么表的行为如此我通过替换
cell.layoutIfNeeded()
cell.updateConstraintsIfNeeded()
cell.layoutSubviews()
与
您可以更好地使用autolayout自动计算单元格高度,并删除此方法
func tableView(u tableView:UITableView,heightForRowAt indexath:indexPath)->CGFloat{
。很抱歉不够清楚,我更新了帖子。
cell.layoutIfNeeded()
cell.layoutSubviews()
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()