Sayali Shinde
Sayali Shinde

Reputation: 96

How to always display 1st item of collection view, if collection view is embedded in table view in ios

I have one table view and in custom cell of table view I have one collection view. Collection view has horizontal scrolling with multiple images. On first cell of table view I have scrolled 3 to 4 images from collection view, means I am on 1st row of table view and on 4th image of collection view with horizontal scrolling. Now when I go to or scroll down table view to 5th row, collection view directly take me to image at index which I scrolled in collection view previously. I should be at 1st index of collection view every time I scroll table view.

Upvotes: 0

Views: 545

Answers (2)

Sayali Shinde
Sayali Shinde

Reputation: 96

use this in willDisplayCell method of table view.

let point = CGPoint(x: 0, y: 0)

tableViewCell.collectionView.setContentOffset(point, animated: true)

Upvotes: 1

lubilis
lubilis

Reputation: 4170

Use prepareForReuse() method in custom TableView cell and reset UICollectionView scroll.

Note: this approach will work but scroll inset is always resetted. So, in your example, if you scroll down and then return to first cell, then it won't showing 4th image but will show the first. If you want to achieve a better solution, create an array of double to remember internal UICollectionViews cells contentInsets and then use these values in prepareForReuse method to set right scroll on UICollectionView. This approach could be expensive for memory usage.

Upvotes: 0

Related Questions