表内的集合视图不会立即在 swift 3 中加载 [英] Collection View inside Table does not load immediately in swift 3

查看:16
本文介绍了表内的集合视图不会立即在 swift 3 中加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Home View Controller中添加了一个表格视图.
在表格视图中,我添加了集合视图.
首次运行应用程序时集合视图内的图像.然后,转到其他链接并返回主页,但集合视图中的图像不会立即显示,直到它第二次滚动.
多次拉视图后,出现图像.
Collection.swift"中,
ViewDidLoad()中,

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

我还添加了 viewWillLayoutSubviews()

override func viewWillLayoutSubviews() {

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

我还重新加载收藏视图

 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
}



表格视图单元格 (CollectionCell.swift) 文件中,


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


}

如何在 swift 3 中立即加载和显示?谁能帮帮我?

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

推荐答案

你不能依赖调用的顺序.当前,您正在尝试将 setIndex 从表视图的 cellForRowAtIndexPath 传递给集合视图的委托方法.解决此问题的一种简单方法是,不要使用单个变量来传递行号,而是将其传递到集合视图的 tag 属性中.然后每个集合视图都会知道它的相关行号.即

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.

在表格视图的 cellForRowAtIndexPath 中:

In the table view's cellForRowAtIndexPath:

cell.collectionView.tag = indexPath.row

然后在collection view的numberOfItemsInSection中:

And then in the collection view's numberOfItemsInSection:

return self.model.sets[collectionView.tag].subsets!.count

这篇关于表内的集合视图不会立即在 swift 3 中加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆