0

I am using a collection view inside another collection view cell. Now I want to scroll to an indexPath of inner collection View. Help me if you know this.Nested CollectionView - Need scroll to red cell

Based on the above image, I want to auto scroll the collection view to the red cell which is inside the nested collection view. Assume the nested collection view is a section in the Main collectionView.

  • [scrollToItem](https://developer.apple.com/documentation/uikit/uicollectionview/1618046-scrolltoitem) might be what you want. Possible duplicate of [Scrolling to first cell](https://stackoverflow.com/questions/32262566/uicollectionview-scrolling-to-first-cell/32262690#32262690) – Miket25 Jan 05 '18 at 02:43
  • scrollToItem work for the main collection view. I want to scroll to child collection view index path 0,2 i.e. red color cell in the above picture. – SelvamSankarans Jan 05 '18 at 03:43
  • Is it the case where you cannot have a reference to the inner collection view? – Miket25 Jan 05 '18 at 17:09

1 Answers1

0

Try using a reference to this collection view.

Say that you UICollectionView containing the inner UICollectionView is named YourInnerCollectionViewCell, it will looks something like:

let innerCollectionViewCellId = "innerCollectionViewCellId"

class MainCollectionView : UICollectionViewController {

    var innerCollectionViewCell: YourInnerCollectionViewCell?

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView?.delegate = self
        collectionView?.dataSource = self
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        switch indexPath.row {
        case 2:
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: innerCollectionViewCellId, for: indexPath) as! YourInnerCollectionViewCell
            self.innerCollectionViewCell = cell
            return cell
        default:
            // return other cell
        }
    }

    ....

}

class YourInnerCollectionViewCell: UICollectionViewCell {

    override init(frame: CGRect){
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

Now that you have a reference to this cell you can access the collectionView in it and perform the scrollTo.

Hope it help.

Florian Ldt
  • 1,125
  • 3
  • 13
  • 31