【发布时间】:2017-02-26 14:03:34
【问题描述】:
我想要实现的是:单击其中的标签时,单元格可以展开/折叠。应该记住单元格大小,以便当用户滚动回单元格时,它应该是以前的大小。我使用自动布局设置单元格,所有视图都分配了高度约束。当用户单击标签时,我通过更改标签的高度约束来更改单元格高度:
func triggerExpandCollapse(_ cell: UITableViewCell) {
guard let cell = cell as? ActivityTableViewCell, let indexPath = self.activitiesTableView.indexPath(for: cell) else { return }
if !self.expandedCellIndex.insert(indexPath.row).inserted {
self.expandedCellIndex.remove(indexPath.row)
cell.descLabelHeightConstraint.constant = 60
}
else {
cell.descLabelHeightConstraint.constant = cell.descLabel.intrinsicContentSize.height
}
self.activitiesTableView.beginUpdates()
self.activitiesTableView.endUpdates()
}
并在返回每个单元格之前配置单元格的高度:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: Constants.TableViewCellID.activity, for: indexPath)
guard let activityCell = cell as? ActivityTableViewCell else {
return cell
}
activityCell.delegate = self
if self.expandedCellIndex.contains(indexPath.row) {
activityCell.descLabelHeightConstraint.constant = activityCell.descLabel.intrinsicContentSize.height
}
else {
activityCell.descLabelHeightConstraint.constant = 60
}
activityCell.layoutIfNeeded()
return activityCell
}
我还通过以下方式启用自动布局:
self.activitiesTableView.rowHeight = UITableViewAutomaticDimension
self.activitiesTableView.estimatedRowHeight = 1000
到目前为止,除了最后一个单元格之外,单元格可以正常滚动行为展开/折叠!当我展开最后一个并向上滚动时,activitiesTableView 看起来“跳跃”了一小段距离。详情可以参考video
我现在不知道如何调试。有没有人对此有一些想法?谢谢。
【问题讨论】:
标签: ios swift uitableview