Collection view inside a collection view

Viewed 44

As of title, I'm having trouble with the issue of nesting collectionViews. I found a workaround gimmicky solution but I'm not really satisfied with the result:

class ViewController3: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var outCollection: UICollectionView!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    outCollection.dataSource = self
    outCollection.delegate = self
  }
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: 
IndexPath) -> UICollectionViewCell {
let cell = outCollection.dequeueReusableCell(withReuseIdentifier: "outCell", for: 
indexPath) as! CollectionViewCell
cell.backgroundColor = .green
cell.setViewDD() // gimmick HERE
return cell
  }
}

and for the CollectionViewCell

class CollectionViewCell: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var inCollection: UICollectionView!
func setViewDD() {
    self.inCollection.delegate = self
    self.inCollection.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 4
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = inCollection.dequeueReusableCell(withReuseIdentifier: "inCell", for: indexPath) as! innercellulosa
    cell.backgroundColor = .red
    return cell
  }
}

if I try to set the delegate and datasource of the innerCollection (either in VC3 or CVCell) I stumble in multiple problems which I can't solve.

0 Answers
Related