问题
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?
来源:https://stackoverflow.com/questions/44541403/collection-view-inside-table-does-not-load-immediately-in-swift-3