iVvaibhav
iVvaibhav

Reputation: 105

Multiple collection view in table view scrolling at the same time

I have collection view as rows of a tableview. The collection view only supports horizontal scrolling. When I scroll to the end of the collection view in the first row, and then scroll to the bottom of the tableview. The last collection view is also at the end position when I haven't even touched that yet.

I know that the issue is because of dequeueReusableCell of the tableview. I have tried using the prepareForReuse() in UITableViewCell.

I expect that whenever any other row of the table view should remain unaffected by the interaction done on rest of the table view rows.

Upvotes: 0

Views: 1687

Answers (1)

André Slotta
André Slotta

Reputation: 14030

You could store (and restore) the collection views' content offsets by using the table view's delegate methods:

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
}

Upvotes: 7

Related Questions