Mehul Makwana
Mehul Makwana

Reputation: 11

Nested collection view Swift 4.2

I have view controller which has one horizontal collection view which has three cells. Inside each cell I have one vertical collection view which has 2 section. 1st section has one cell and 2 nd section is Dynamics It can have any number of cells. In first section of vertical collection view which has one cell it has one horizontal collection view which can have any number of cells.

Issue : when innermost collection view which is inside first section of vertical collection view is scrolling, and when it reaches end of collection view I want to disable the scrolling so that my outer most collection view which has three cells can scroll, and immediately after that I want to enable the scrolling of innermost collection view. So that it can scroll again. It's like scroll view or collection view

Nested Collection View

Upvotes: 0

Views: 1733

Answers (1)

Pooja Kamath
Pooja Kamath

Reputation: 1298

enter image description here

Here is a similar example ,which has collection view within another collection view cell

GitHub sample

Here I am trying to get the last cell displayed event in the inner collection view and disabling the scroll.

override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        //Checking if its not the inner collection view
        if(collectionView.restorationIdentifier != "Collection")
        {
            //scrolled till datasource.count -1 == 14 in our case
            if( indexPath.row == 14){

                //Disabling the scrolling
                collectionView.isScrollEnabled = false;

                //Saved the collection view , so that the scrolling can be enabled later.
                scrolldisabledCollectionView = collectionView;
            }
        }

    }

And when the outer collection view is scrolled, The inner collection view is enabled.

 override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView)
    {
        if(collectionView?.restorationIdentifier == "Collection")
        {
            scrolldisabledCollectionView?.isScrollEnabled = true
        }
    }

Upvotes: 0

Related Questions