表格单元格内的 UICollection 视图不显示 Swift 3 中的图像 [英] UICollection View inside Table Cell does not show images in Swift 3

查看:20
本文介绍了表格单元格内的 UICollection 视图不显示 Swift 3 中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 UICollectionView 放在主页的表格单元格中.每个类别行都有其内容图像.
但是,我的问题是,当我运行该应用程序时,每当我运行该应用程序时,第二行/第二类别下的图像都不会显示.
单击更多"按钮后将显示图像,并将转到详细信息 UI.然后,我单击返回"按钮并到达主页.此时,图像显示正常.
我尝试了很多代码,但我不知道哪里错了.

I put UICollectionView inside table cell in Home page. Each category row has their contents image.
But, my problem is that when I run the app, the images under second row/second category do not show whenever I run the app.
The images will show after I click more button and it will goes to details UI. Then, I click on Back button and reach the Home page. At this time, the images shows properly.
I tried many codes but I don't know where it is wrong.

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    tableView.reloadData()
    self.tableView.reloadData()
    self.tableView.layoutIfNeeded()
}

override func viewDidAppear(_ animated: Bool) {
    tableView.reloadData()
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    //getSearchWords()
    DispatchQueue.main.async {
        self.tableView.reloadData()

    }
    self.tableView.reloadData()
    self.tableView.layoutIfNeeded()
}

    override func viewDidLoad() {

     DispatchQueue.main.async {
       self.tableView.reloadData()
     }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

   // debugPrint("HOme Table View Cell")

    let cell = tableView.dequeueReusableCell(withIdentifier: "homecell") as! HomeCategoryRowCell
    let list = sections[(indexPath as NSIndexPath).row]

    cell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
    cell.collectionViewOffset = self.storedOffsets[indexPath.row] ?? 0

    DispatchQueue.main.async {
        cell.categoryTitle.text = list.package_name
        cell.mainAssociatedURL.text = list.package_url
    }
    cell.categoryTitle.font = UIFont.boldSystemFont(ofSize: 17.0)
    cell.collectionView.tag = indexPath.row
    cell.parentVC = self
    cell.collectionView.reloadData()

    return cell
}


extension Home : UICollectionViewDelegate, UICollectionViewDataSource {

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

    return sections[collectionView.tag].packageTable.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoCell", for: indexPath) as! HomeVideoCell

        let list = sections[collectionView.tag].packageTable[indexPath.row]

        if(list.poster_image_url != StringResource().posterURL){
            let url = NSURL(string: list.poster_image_url)
            do{
                if let url = url {
                    _ = try Data(contentsOf:url as URL)
                    poster_url = list.poster_image_url
                }
            }catch let error {
                //debugPrint("ERRor ::(error)")
                poster_url = StringResource().posterURL
            }
        }else{
            poster_url = StringResource().posterURL
        }


        AsyncImageLoader.sharedLoader.imageForUrl(urlString: poster_url) { (image, url) -> () in
            DispatchQueue.main.async(){

                cell.movieTitle.text = list.name
                if(url == StringResource().posterURL){
                    cell.imageView.image = UIImage(named: "sample_cover")
                }else{
                    cell.imageView.image = image
                }
            }
        }

  //  cell.layer.shouldRasterize = true
  //  cell.layer.rasterizationScale = UIScreen.main.scale

    return cell

}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    print("Home Collection view at row (collectionView.tag) selected index path (indexPath)")

    let list = sections[collectionView.tag].packageTable[indexPath.row]
    self.prefs.set(list.poster_image_url, forKey: "poster_image_url")
    self.prefs.set(list.name, forKey: "name")
    self.prefs.set(list.assets_id, forKey: "VEDIO_ID")

    debugPrint(list.name)
    debugPrint(list.assets_id)


}
}

以上这些代码是用 Home.swift 编写的.

HomeCell.swift中,

In HomeCell.swift,

class HomeCell : UITableViewCell {


var parentVC: UIViewController?

@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var categoryTitle: UILabel!
@IBOutlet weak var SeeAll: UIButton!
@IBOutlet weak var mainAssociatedURL: UILabel!

let prefs:UserDefaults = UserDefaults.standard
var catId: String! = ""
var associated_url: String!

override func awakeFromNib() {
    super.awakeFromNib()

    SeeAll.setTitle(NSLocalizedString("See All >", comment: ""), for: .normal)
    SeeAll.titleLabel!.font = UIFont (name: "Tharlon", size: 14)

}
@IBAction func btnSeeAll(_ sender: Any) {

    catId = categoryTitle.text!
    associated_url = mainAssociatedURL.text!

    self.prefs.setValue(1, forKey: "PROVIDER_ID")
    self.prefs.set(catId, forKey: "PROVIDER_NAME")
    self.prefs.set(associated_url, forKey: "associated_url")
}

override func layoutSubviews() {
    super.layoutSubviews()
    collectionView.collectionViewLayout.invalidateLayout()
}
}

extension HomeCategoryRowCell {
    func setCollectionViewDataSourceDelegate<D: UICollectionViewDataSource & UICollectionViewDelegate>(_ dataSourceDelegate: D, forRow row: Int) {

        self.collectionView.delegate = dataSourceDelegate
        self.collectionView.dataSource = dataSourceDelegate
        self.collectionView.tag = row
        self.collectionView.setContentOffset(self.collectionView.contentOffset, animated:false) // Stops collection view if it was scrolling.
        self.collectionView.reloadData()
}

var collectionViewOffset: CGFloat {

    get { return collectionView.contentOffset.x }

    set { collectionView.contentOffset.x = newValue }
}
}

有人可以帮我吗?

推荐答案

像下面这样编辑控制器,你调用了太多的 reloadData.

Edit controller like below, you are calling too many reloadData.

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    self.tableView.layoutIfNeeded()
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

//getSearchWords()
    DispatchQueue.main.async {
        self.tableView.reloadData()
    }
    self.tableView.layoutIfNeeded()
}

override func viewDidLoad() {
        super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
}

并添加:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            tableView .deselectRow(at: indexPath, animated: false)  
}

这篇关于表格单元格内的 UICollection 视图不显示 Swift 3 中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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