UICollectionView 没有在 UITableViewCell 内更新

Posted

技术标签:

【中文标题】UICollectionView 没有在 UITableViewCell 内更新【英文标题】:UICollectionView not updating inside UITableViewCell 【发布时间】:2015-11-17 12:01:35 【问题描述】:

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

我为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】:

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

【讨论】:

这应该是公认的答案。我遇到了完全相同的问题,而这一班轮做到了这一点(仔细想想,这很明显)。

以上是关于UICollectionView 没有在 UITableViewCell 内更新的主要内容,如果未能解决你的问题,请参考以下文章

UICollectionViewCells 没有出现在 UICollectionView 中

ALAsset 指针在访问时抛出 EXC_BAD_ACCESS

UICollectionView:在没有任何项目的部分中显示标签“无项目”

UICollectionView 没有正确滚动

UICollectionView 委托方法没有被正确调用

UICollectionView 没有滚动到位置