tableviewcell 中的collectionview。当 tableview 重用单元格时。 collectionview 在tableivewcell 中保持位置。这是错误?以及如何分离它。示例:单元格 1 和单元格 10 是 Collection View (水平)。当我滚动单元格 1 时,单元格 10 也滚动相同的位置。请帮助我。
最佳答案
创建一个 Dictionary
来记住每个单元格的偏移量,例如:
var contentOffset: [Int: CGFloat] = [:]
当表格 View 单元格从屏幕中移除时,存储您的 Collection View 的偏移量,如
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let cell = cell as? YouTableViewCell else {
return
}
contentOffset[indexPath.row] = cell.collectionView.contentOffset.x
}
现在,当单元格即将显示时,恢复内容偏移量,如下所示:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let cell = cell as? YouTableViewCell else {
return
}
cell.collectionView.contentOffset.x = contentOffset[indexPath.row] ?? 0
}
关于ios - tableww 单元格中的 Collectionview。这是重用的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50696828/