Collection View inside Table does not load immediately in swift 3
Asked Answered
M

1

0

I add one table view in Home View Controller.
Inside table view, I add Collection View.
An Image inside collection view at first time running the app. Then, goes to other links and come back to Home but image in the collection view does not show immediately until it scrolls at this second time.
After pulling the view several times, then Image appears.
In "Collection.swift" ,
In ViewDidLoad() ,

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

I also add in viewWillLayoutSubviews()

override func viewWillLayoutSubviews() {

    debugPrint("viewWillLayoutSubviews")
    DispatchQueue.main.async(execute: {() -> Void in
        self.tableView.reloadData()
        self.tableView.layoutIfNeeded()
    })   
}

and I also reload the Collection View

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

    let mainThemeList = mainHomeThemeTable[(indexPath as NSIndexPath).row]
    self.prefs.set(mainThemeList.main_associated_url, forKey: "associated_home_url")
    let cell = tableView.dequeueReusableCell(withIdentifier: "homecell") as! HomeCategoryRowCell

    DispatchQueue.main.async {
        cell.categoryTitle.text = mainThemeList.main_name
        cell.mainAssociatedURL.text = mainThemeList.main_associated_url
        cell.categoryTitle.font = UIFont.boldSystemFont(ofSize: 17.0)
        cell.collectionView.reloadData()

    }

    return cell
}



In Table View Cell (CollectionCell.swift) file ,

override func awakeFromNib() {
   self.collectionView.reloadData()
   self.collectionView.setNeedsLayout()
   self.collectionView.layoutIfNeeded() } 


override func layoutSubviews() {
    super.layoutSubviews()
     self.collectionView.reloadData()
     self.collectionView.setNeedsLayout()
     self.collectionView.layoutIfNeeded()


}

How can I do to load and show immediately in swift 3? Can anyone help me?

Mortmain answered 14/6, 2017 at 9:56 Comment(4)
collectionView.reloadData() in custom cell of table view so easily reload with table view reload if not load then leave comment i will give you another suggetion And Code Into View DidAppear()Balance
Bro @BHAVIKPANCHAL, I add tableView.reloadData() in viewDidAppear. Now, when the app runs, the image shows immediately. But, when I go to other pages and goes back to Home, then image does not show and need to pull to refresh.Mortmain
bro @BHAVIKPANCHAL, I updated my question. Could you please take a look bro ?Mortmain
I think you can solve it this way https://mcmap.net/q/158507/-uicollectionview-inside-a-uitableviewcell-dynamic-heightInfernal
F
1

You cannot rely on the sequence of the calls. Currently you are trying to pass setIndex from the table view's cellForRowAtIndexPath to the collection view's delegate methods. A simple way to fix this is rather than using a single variable to pass the row number, instead pass it in the collection view's tag property. Then each collection view will know it's relevant row number. i.e.

In the table view's cellForRowAtIndexPath:

cell.collectionView.tag = indexPath.row

And then in the collection view's numberOfItemsInSection:

return self.model.sets[collectionView.tag].subsets!.count
Famed answered 20/11, 2021 at 20:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.