Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
551 views
in Technique[技术] by (71.8m points)

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

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.

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)


}
}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...