1

I'm trying to embed a collection view in a custom UITableViewCell I am using as the HeaderView of each of my table view cells. This is the hierarchy I am using in the storyboard

Hierarchy of the story board

And this is the code of the Custom Header Table View Cell. The collection view is connected from the storyboard to the class, I am setting the delegate and the datasource in the table view cell and I'm implementing all the protocols required in CollectionView Data Source. For testing purposes I simply hardcoded the number of items to ten and the cell should simply show a label, with a text of "test" and a background color of red

var headerDelegate:HeaderTableViewCellDelegate?

var timeInterval:TimeInterval?

@IBOutlet private weak var collectionView: UICollectionView!

var exercise:ExerciseRealm!

var workout:Workout!

@IBOutlet weak var collectionLayout: UICollectionViewFlowLayout! {
    didSet {
        collectionLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
    }
}

@IBOutlet weak var exerciseNameLabel: UILabel!
override func awakeFromNib() {
    super.awakeFromNib()
    self.collectionView.delegate = self
    self.collectionView.dataSource = self
    //TODO: need to setup collection view flow layout
     let flowLayout = UICollectionViewFlowLayout()
     flowLayout.scrollDirection = .horizontal
     flowLayout.itemSize = CGSize(width: 70, height: 80)
     flowLayout.minimumLineSpacing = 5.0
     flowLayout.minimumInteritemSpacing = 5.0
     self.collectionView.collectionViewLayout = flowLayout


}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 10
}

func numberOfSections(in collectionView: UICollectionView) -> Int {

    return 1
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewID", for: indexPath) as! HeaderCollectionViewCells

    cell.updateCellWithIndicator(indicator: "test")
    cell.backgroundColor = .red

    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
}

In my main TableViewController, I am showing this cell as a header view with the following code

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell") as! HeaderTableViewCell

    guard let workout = workout else {print("workout is nil in viewForHeaderInSection");return cell}

    let exercises = workout.exercisesList

    cell.headerDelegate = self
    cell.exercise = exercises[section]
    cell.workout = workout

    return cell


}

Although I have already set the data source and delegate from the collection view to the custom header table view cell, both from storyboard and in code, the collection view is still not showing. every other data in the header view is being shown as its supposed to be so I really don't know what is missing when implementing the collection view.

Francisco Ruiz
  • 209
  • 3
  • 12
  • Is the `numberOfItemsInSection ` in the collectionView at least called or none of the `collectionView` methods called? – Hudi Ilfeld Jun 02 '20 at 15:39
  • Provide a [minimal-reproducible-example](https://stackoverflow.com/help/minimal-reproducible-example) of your issue. – Frankenstein Jun 02 '20 at 15:56

0 Answers0