UICollection View inside Table Cell does not show images in Swift 3

这一生的挚爱 提交于 2019-12-18 09:32:12

问题


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)


}
}

These above codes are written in Home.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 }
}
}

Can anyone help me please?


回答1:


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
}

and add:

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


来源:https://stackoverflow.com/questions/45074376/uicollection-view-inside-table-cell-does-not-show-images-in-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!