1

I am trying to create a menu where top horizontal scrolling view contains the menu categories. When I tap on one of the menu categories, ads related to the category appears.

When First is selected

--**First**---|--Second--|---Third--|--Fourth--| (scrolling categories)
------------------------------------------------------------------
\\\FirstA\\\    \\\FirstB\\\   \\\FirstC\\\     \\\FirstD\\\
\\\FirstA\\\    \\\FirstB\\\   \\\FirstC\\\     \\\FirstD\\\
------------------------------------------------------------------

When Second is selected

--First---|--**Second**--|---Third--|--Fourth--| (scrolling categories)
------------------------------------------------------------------
\\\SecondA\\\    \\\SecondB\\\   \\\SecondC\\\     \\\SecondD\\\
\\\SecondA\\\    \\\SecondB\\\   \\\SecondC\\\     \\\SecondD\\\
------------------------------------------------------------------

When I tap on First Category, data related to first comes as a collectionView. Similarly, when I tap on second Category, data related to second comes in the UICollectionView.

Now I am thinking of doing it in a UITableViewCell with two UICollectionViews. First UICollectionView will contain categories and second UICollectionView will contain data related to categories.

But I have never used two UICollectionViews in a single UITableViewCell. So I am asking is it the correct approach for this type of requirement or should I do it in some other manner.

Sudhanshu Gupta
  • 2,255
  • 3
  • 36
  • 74

2 Answers2

0

You could subclass an UITableViewCell, add UICollectionView as subview, conform UICollectionViewDataSource and UICollectionViewDelegate

here's sample code using xib

class CollectionViewTVC: UITableViewCell {
    @IBOutlet weak var collectionView: UICollectionView!
}

extension CollectionViewTVC: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // return items
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        // return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
        // return size
    }
}

extension CollectionViewTVC: UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        // select cell action
    }
}
0

Why don’t you use two table view cells, one for every collectionViewu?

zvone
  • 39
  • 2
  • well eventually I have used two tableviewcells, just want to know if it can be done in a single tableviewcell – Sudhanshu Gupta Oct 08 '17 at 14:49
  • I think this isn’t the best Way but you can add in xib two CollectionViews one below another, make two custom class that conform to delegate and Data source. Connect both collectionViews to outlets in cell and instantie theirs datasources and delegates(ones that you Made earlier), you can also make just one extra Data source and delegate and for second collectionView conform that cell, but I think first solution is little cleaner – zvone Oct 08 '17 at 15:05