简体   繁体   English

在将UICollectionView的数据添加到屏幕之前,先对其进行加载

[英]Load A UICollectionView's Data Before It's Added To The Screen

I have a situation where I need to have a UICollectionView have its data loaded (so I can fetch its content size) before it's been added to the view hierarchy. 我遇到的情况是,在将UICollectionView添加到视图层次结构之前,需要先加载其数据(以便获取其内容大小)。

I have tried calling reloadData and dispatching on the main_thread but to no avail 我试过调用reloadData并在main_thread上调度但无济于事

 //The context manager needs to check with this to see if the scrollview can scroll (because otherwise it can't pan it up)
    func hasEnoughContentForScrolling()->Bool
    {
        dispatch_sync(dispatch_get_main_queue(), {self.collectionView.reloadData()})

        if(collectionView.contentSize.height > UIScreen.mainScreen().bounds.height)
        {
            return true
        }
        return false
    }

Do you know how I can pre-load a UICollectionView before it's been added to the screen? 您知道在将UICollectionView添加到屏幕之前如何预加载它吗?

You can hide your collectionView in your viewDidLoad method and as collectionView is reloaded you can show your collection View with loaded data. 您可以将您的collectionView隐藏在viewDidLoad方法中,并在重新加载collectionView时,可以显示具有已加载数据的collection View。 I tried to write the code in Swift. 我试图用Swift编写代码。 not sure it is the right code for a swift. 不确定这是快速的正确代码。 but may be it can help you to understand- 但也许可以帮助您了解-

override func viewDidLoad() 
{super.viewDidLoad()self.collectionView.hidden = YES}  

func hasEnoughContentForScrolling()->Bool   
{ 
   dispatch_sync(dispatch_get_main_queue(),{
   self.collectionView.reloadData()self.collectionView.hidden = NO })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM