UICollectionView 未在 UITableViewCell 内更新 [英] UICollectionView not updating inside UITableViewCell

查看:30
本文介绍了UICollectionView 未在 UITableViewCell 内更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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.

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

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
    }


}

这是我为 UIViewController 编写的一些代码,其中包含我的 tableView 的数据源方法.tableview 是一个 @IBOutlet

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()

}

推荐答案

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

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.

这篇关于UICollectionView 未在 UITableViewCell 内更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆