表单元格内的UICollection视图不显示Swift 3中的图像

时间:2017-07-13 07:41:31

标签: uitableview swift3 uicollectionview uicollectionviewcell uicollectionviewlayout

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

enter image description here

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 中,

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 }
}
}

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

编辑控制器如下,你调用的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)  
}