10

I have a UITableView Controller class, and inside of it is a String array. The Table view has 1 cell prototype and in it a UICollectionView with 1 cell prototype.

The CollectionView is populated by passing the array into the tableview cell, which is a UICollectionView Delegate and DataSource.

When I make changes to the array inside the TableViewController, and call self.tableView.reloadData(), the CollectionView inside the cell does NOT update.

How do I fix this?

Thank you

EDIT:

Code in TableViewController:

@IBAction func addToArrayAction(sender: AnyObject) {
        self.testArray.append("Ant-Man")
        self.tableView.reloadData()
    }

    var testArray = ["Guardinas", "Iron Man", "Avengers", "Captain America"]

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("collectionContainerCell", forIndexPath: indexPath) as! TableViewCell
        cell.testArray = self.testArray
        return cell
    }

Code in UITableViewCell:

var testArray: [String]?

    override func awakeFromNib() {
        super.awakeFromNib()

        self.collectionView.delegate = self
        self.collectionView.dataSource = self

    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = self.collectionView.dequeueReusableCellWithReuseIdentifier("collectionViewCell", forIndexPath: indexPath) as! CollectionViewCell

        cell.movieTitleLabel.text = self.testArray![indexPath.item]
        cell.movieYearLabel.text = "2016"

        return cell
    }
Dino Prašo
  • 601
  • 9
  • 22
  • Could you please show your cellForRowAtIndexPath code? – Gagan_iOS Nov 14 '15 at 21:52
  • @Gagan_iOS There. And I've checked this with a breakpoint. I DOES get called again, when .realodData() is called. – Dino Prašo Nov 14 '15 at 21:55
  • you display only one cell in tableview ? – nsinvocation Nov 14 '15 at 22:03
  • ok..you are calling reloadData for tableView when you make chenges in array and obviously table is reloading but what about collectionView reolad. You are showing data from array in collectioview so you have to reload collctionView otherwise there will be no update. – Gagan_iOS Nov 14 '15 at 22:03
  • @hal9000 for now, until I make it work, yes – Dino Prašo Nov 14 '15 at 22:03
  • @Gagan_iOS Okay. Fair enough. But where do I call the collectionView.reloadData() ? – Dino Prašo Nov 14 '15 at 22:04
  • 2
    If you are using only one cell in tableView then you can reload in cellForRowAtIndexPath method of tableViiew else you can can call after calling of reloadData of TableView. – Gagan_iOS Nov 14 '15 at 22:06
  • @hal9000 there is only one TABLEVIEW cell. Inside of it is a collectionView that displays cells for the array. – Dino Prašo Nov 14 '15 at 22:06
  • @Gagan_iOS calling collectionView.reloadData inside cellForRowAtIndexPath worked. Maybe submit answer so I can mark it as answered – Dino Prašo Nov 14 '15 at 22:08
  • **You can easily reload nested `UICollectionView`:** https://stackoverflow.com/a/58261035/5693826 – ZAFAR007 Oct 07 '19 at 09:12

2 Answers2

5

Please try to call reloadData of UICollectionView on every update in array. If you are using only one cell in UITableView then you can reload in cellForRowAtIndexPath method of UITableView else you can can call after calling of reloadData of UITableView.

jaytrixz
  • 4,059
  • 7
  • 38
  • 57
Gagan_iOS
  • 3,638
  • 3
  • 32
  • 51
  • 1
    I have similar scenario : A screen has UITableView whose UITableViewCells has UICollectionView inside it. Datasource,delegate of both UITableView, UICollectionView is the viewController. Inside the CellForRowAtIndexPath: datasource method of UITableView, I am calling 'reloadData' on collectionView which is a subview of that tableview cell. Surprisingly, reload is happening properly only if I call [collection reloadData] inside dispatch_asyn with mainQueue. Calling reload without dispatch_async is not refreshing collectionView – NaveenRaghuveer Aug 30 '16 at 05:43
-1

After I added cell.collectionView.reloadData(), CollectionView scrolling is so slow in my project. How to solved that kind of problem.