Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Swift ReloadData on UITableView does not refresh UICollectionView inside of UITableViewCell

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
    }
like image 223
Dino Prašo Avatar asked Nov 14 '15 21:11

Dino Prašo


1 Answers

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.

like image 93
Gagan_iOS Avatar answered Nov 15 '22 20:11

Gagan_iOS