6

I am nesting a collection view inside of a table view cell. I need to implement a screen similar to that of the app store of netflix. Where you have categories such as enter image description here

I would need to load the data from a JSON, for instance - "Popular" may have 11 videos, "Action" May have 3 videos and "Adventures" may have 20. So inside each table view cell, that specific collection view would load a different number of collection view cells depending on how many videos are in each category (Popular, Action, Adventure), which will be displayed within its own respective horizontal collection view.

So far with my code i display each category title in the table view header. But within that category, inside the collection view numberOfItemsInSection and cellForItemAt, i can't find a way to load each collection view interdependently. For example, collection view 1 that relates to Popular, should load 11 cells, Collection View 2 which relates to Action should load 3 cells. Below is my code so far

My TABLE VIEW METHODS WHICH ARE INSIDE OF VIEW CONTROLLER

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    guard let cell = tableView.dequeueReusableCell(withIdentifier: "categoriesCell") as? CategoriesTableViewCell else { return UITableViewCell() }

    return cell
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    return arrayOfCategoryObjects[section].title
}

func numberOfSections(in tableView: UITableView) -> Int {
    return arrayOfCategoryObjects.count
}

 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    if let cell = cell as? CategoriesTableViewCell {


        cell.moviesCollectionView.dataSource = self
        cell.moviesCollectionView.delegate = self

        cell.moviesCollectionView.reloadData()

    }
}

MY TABLE VIEW CELL

class CategoriesTableViewCell: UITableViewCell {

@IBOutlet weak var moviesCollectionView: UICollectionView!

}

MY COLLECTION VIEW DELEGATES WHICH ARE ALSO INSIDE THE VIEW CONTROLLER

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

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
   // print(arrayOfCategoryObjects[section].movies[section])

    return arrayOfCategoryObjects[section].movies.count

  }

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


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

        cell.movieTitleLbl.text = arrayOfCategoryObjects[indexPath.item].movies[indexPath.item]

        return cell

  }

MY COLLECTION VIEW CELL

class MoviesCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var movieTitleLbl: UILabel!

}
9
  • This is the best resource you will get on this topic. See if this helps you else will try to look into the lengthy question :P Commented Jan 24, 2020 at 17:34
  • @Josh thanks for the response. I have actually been looking at that already. however its not showing me a way to display a different number of collection view cells per table view cell. Right now, all of the collection view cells are displaying the same count. For example - i have 4 categories (which are table view cells) - and each category should display a diff number of collection view cells. but they all display 3 collection view cells Commented Jan 24, 2020 at 17:38
  • don't you think this is always same for you - return arrayOfCategoryObjects[section].movies.count ? Commented Jan 24, 2020 at 17:40
  • ok, let me try to guide in an answer. Commented Jan 24, 2020 at 17:41
  • @CodingwhileLoading you do everything in code ( better) ? what is the exact problem ? at glance calling reloadData in willDisplay seems wrong. collectionView inside tableViewCell is just a view. think simple. Commented Jan 24, 2020 at 17:42

1 Answer 1

8

You need to set a tag for your UICollectionView as parent UITableViewCell's indexPath.section:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    if let cell = cell as? CategoriesTableViewCell {

        cell.moviesCollectionView.dataSource = self
        cell.moviesCollectionView.delegate = self
        cell.moviesCollectionView.tag = indexPath.section
        cell.moviesCollectionView.reloadData()

    }
}

and then in your UICollectionView's delegate:

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

    return arrayOfCategoryObjects[collectionView.tag].movies.count

  }

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

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

    cell.movieTitleLbl.text = arrayOfCategoryObjects[collectionView.tag].movies[indexPath.item]

    return cell

  }

Hope this helps!

Sign up to request clarification or add additional context in comments.

11 Comments

thank you will try to implement right now and get back to you
i am still getting 3 collection view cells per row. The expected ouput should be 4 table view cells, and within each table view cell the number of collection view cells should be 3, 3, 5, 5. - Thats 3 collection view cells in the first table view cell, 3 in the 2nd table view cell, 5 in the third table view cell and 5 in the last table view cell
print the collectionView.tag in the second method above.
i just did and the output is 0
I think that means it is getting the tag of the first collection view (which is 0) and is stopping there
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.