1

I am building a tvos app. Currently i am stuck on a problem in which i have a UITableView. There are two sections of my UITableView and each section is having only one row. In both of these rows i have a UICollectionView which scrolls horizontally. In each UICollectionViewCell there is an imageView which is used to show some image. here is the image of what i am setting up. StoryBoard

I have set the a separate reuse Identifier for both the UITableViewCells. However i have the same reuse identifier for the UICollectionViewCells.

Now i am fetching data from the web. In the start there is no data so the UITableView and the UICollectionViews are empty. As soon as the data becomes available i call tableView.reloadData() but calling this function does not bring the new information to the collection view. Can someone guide me how i can get this working.

Here is my code for the custom UITableviewCell Class.

import UIKit

class UIScrollingTableViewCell : UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {


    @IBOutlet weak var myCollectionView: UICollectionView!


    //MARK: CollectionViewDelegate & DataSource

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        print("numberOfSectionsInCollectionView ")
        return 1
    }


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

        if self.reuseIdentifier == recentlyWatchCellId {
            return DataManager.sharedInstance.recentlyWatched.count
        } else{
            return DataManager.sharedInstance.recentlyAdded.count;
        }

    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("smallCollectionViewCell", forIndexPath: indexPath) as! MyCollectionViewCell
        var lesson = DataManager.sharedInstance.recentlyWatched[indexPath.row]
        if self.reuseIdentifier == recentlyAddedCellId {
            lesson = DataManager.sharedInstance.recentlyAdded[indexPath.row]
        }

        if let url = lesson.instructorImageUrl {
            cell.imageView.sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "categoryLessons"))
        }
               return cell
    }


    //MARK: Focus
    override func canBecomeFocused() -> Bool {
        return false
    }


}

and here is some of my code for the UIViewController which has the datasource methods for my tableView. tableview is an @IBOutlet

//MARK: UITAbleViewDelegate & UITableViewDatasource

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    switch section {
        case RecentLessonSection.Recorded.rawValue:
            return NSLocalizedString("Recently Added", comment: "Recently Added")

        case RecentLessonSection.Watched.rawValue:
            return NSLocalizedString("Recently Viewed", comment: "Recently Viewed")
        default:
            return ""
    }
}


func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 2;
    //return RecentLessonSection.Max.rawValue
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell : UIScrollingTableViewCell
    switch indexPath.section {
        case RecentLessonSection.Recorded.rawValue:
            cell = tableView.dequeueReusableCellWithIdentifier(recentlyAddedCellId, forIndexPath: indexPath) as! UIScrollingTableViewCell

        case RecentLessonSection.Watched.rawValue:
            cell = tableView.dequeueReusableCellWithIdentifier(recentlyWatchCellId, forIndexPath: indexPath) as! UIScrollingTableViewCell
    default:
        cell = tableView.dequeueReusableCellWithIdentifier(recentlyWatchCellId, forIndexPath: indexPath) as! UIScrollingTableViewCell


    }


    return cell
}

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



//MARK : Notification Handlers
func receivedNewData() {
        self.tableView.reloadData()

}
Madu
  • 4,849
  • 9
  • 44
  • 78

1 Answers1

3

In the code that you provide I can see that you called reloadData() for the root table view only. But if you want to update a collection view you should also call reloadData() for each row that contains a collection view inside. For example, you can do it in the method func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell. Add line cell.myCollectionView.reloadData() to the end of this method and I hope that it will work for you.

Roman Podymov
  • 4,168
  • 4
  • 30
  • 57
  • This should be the accepted answer. I had the exact same problem and this one liner did the trick (which is pretty obvious when you think about it). – Mauricio Chirino Jan 22 '18 at 14:15