表视图中的多个集合视图同时滚动

iVv*_*hav 0 uitableview ios uicollectionview swift

我将集合视图作为表格视图的行。集合视图仅支持水平滚动。当我滚动到第一行中集合视图的末尾,然后滚动到表视图的底部时。当我还没有触及时,最后一个集合视图也位于结束位置。

我知道问题是由于dequeueReusableCell表格视图造成的。我尝试过使用prepareForReuse()in UITableViewCell.

我希望每当表视图的任何其他行都应该不受表视图其余行上完成的交互的影响。

And*_*tta 7

您可以使用表视图的委托方法来存储(和恢复)集合视图的内容偏移量:

var xOffsets: [IndexPath: CGFloat] = [:]

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    xOffsets[indexPath] = (cell as? TableViewCell)?.collectionView.contentOffset.x
}

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    (cell as? TableViewCell)?.collectionView.contentOffset.x = xOffsets[indexPath] ?? 0
}
Run Code Online (Sandbox Code Playgroud)