0

collectionview in tableviewcell. when tableview reuse cell. collectionview in tableivewcell keep position. this is bug? and how to separate for it. example : cell 1 and cell 10 is collectionview(horizontal). when i scroll cell 1, cell 10 also scroll same postion. please help me.

2 Answers2

4

Create a Dictionary to remember the offset for each cell like:

var contentOffset: [Int: CGFloat] = [:]

When table view cell is being removed from screen store the offset of your collection view like

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
}

Now when cell is about to display restore the content offset like:

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
}
Mukesh
  • 2,792
  • 15
  • 32
0

It is not a bug, when table cell being reused, it also reuse the content which is the collection view which is scrolled. Keep Googling and check out the links:

ios 8 Swift - TableView with embedded CollectionView

https://medium.com/@gargankit476/multiple-collection-view-in-uitableview-ced7909a5af3

DZoki019
  • 382
  • 2
  • 13