UICollectionView不在UITableViewCell内更新

时间:2015-11-17 12:01:36

标签: swift uitableview uicollectionview tableviewcell tvos

我正在构建一个tvos应用。目前我遇到了一个UITableView的问题。我的UITableView有两个部分,每个部分只有一行。在这两行中,我有一个水平滚动的UICollectionView。在每个UICollectionViewCell中都有一个用于显示某些图像的imageView。这是我正在设置的图像。 StoryBoard

我为UITableViewCells设置了单独的重用标识符。但是我对UICollectionViewCells具有相同的重用标识符。

现在我从网上获取数据。在开始时没有数据,因此UITableViewUICollectionViews为空。一旦数据可用,我就会调用tableView.reloadData(),但调用此函数不会将新信息带到集合视图中。有人可以指导我如何使这个工作。

这是我自定义UITableviewCell类的代码。

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
    }


}

这是我的UIViewController的一些代码,它有我的tableView的数据源方法。 tableview@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()

}

1 个答案:

答案 0 :(得分:3)

在您提供的代码中,我可以看到您仅为根表视图调用了reloadData()。但是,如果要更新集合视图,还应该为包含集合视图的每一行调用reloadData()。例如,您可以在方法func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell中执行此操作。将行cell.myCollectionView.reloadData()添加到此方法的末尾,我希望它对您有用。

相关问题