我在我的一个应用程序中使用
UITableView
。在 tableview 的行内,我有水平滚动UICollectionView
。每当用户双击集合视图的单元格时,它会将事件发送回视图控制器以更新数据源。更新数据源后,我使用以下命令重新加载该特定行: tableView.reloadRows(at: [IndexPath(row: index, section: 0)], with: .none)
但我遇到了奇怪的滚动错误。
我想重新加载tableview的行及其内容,即collectionview的内容,没有任何动画或滚动问题。
我尝试了以下解决方案来避免这种情况,但没有成功:(
UIView.performWithoutAnimation {
self.tableView.reloadRows(at: [IndexPath(row: index, section: 0)], with: .none)
}
我什至试图记住表视图和集合视图的内容偏移量,但闪烁仍然存在。如果您遇到过这样的问题,请指导我
这是一个 UI 布局问题。当您重新加载单元格/部分时,它会重新计算其内容大小,因此您的 tableView 内容大小特别是如果您有动态大小的单元格。我建议 2 个解决方案
解决方案一(Hacky):
解决方案二(更好):
我也遇到了完全相同的问题。仅此一点就解决了问题:
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return self.tableView(tableView, heightForRowAt: indexPath)
}
在将图像放入单元格之前,如果可能的话,创建滚动图像的缩略图。
直接在数据更改时更新 tableView 单元格可以解决您的问题。
struct YourModel {
var id: String
var text: String
}
var list: [YourModel] = []
func didUpdated(model: YourModel) {
guard let index = list.firstIndex(where: { $0.id == model.id }) else {
return
}
// Update your list with new data
list[index] = model
// Update table view cell
// use proper IndexPath
let indexPath = IndexPath(row: index, section: 0)
if let cell = yourTable.cellForRow(at: indexPath) as? YourTableViewCell {
cell.label.text = model.text
}
}
尝试 tableView.reloadData() 或者您必须按升序重新排列索引路径,然后重新加载特定的索引路径。