I have to do some operation whenever UICollectionView has been loaded completely, i.e. at that time all the UICollectionView's datasource / layout methods should be called. How do I know that?? Is there any delegate method to know UICollectionView loaded status?
相关问题
- Pass code to a method as an argument
- Get the result of Func<object> when object i
- CollectionView with background Image
- Jquery only works once per page load - want it wor
- UICollectionViewScrollPosition not working
相关文章
- IOS UICollectionview Dynamically change Cell's
- Implicit method group conversion gotcha
-
Compare Delegates Action
- How do I declare a Func Delegate which returns a F
- .NET dll hot swap, no application restart
- Delegate: Method name expected error
- Instead of UICollectionView a black screen is disp
- UICollectionView shouldSelectItemAtIndexPath=NO do
It's actually rather very simple.
When you for example call the UICollectionView's reloadData method or it's layout's invalidateLayout method, you do the following:
Why this works:
The main thread (which is where we should do all UI updates) houses the main queue, which is serial in nature, i.e. it works in the FIFO fashion. So in the above example, the first block gets called, which has our
reloadData
method being invoked, followed by anything else in the second block.Now the main thread is blocking as well. So if you're
reloadData
takes 3s to execute, the processing of the second block will be deferred by those 3s.Def do this:
Then call like this inside your VC
Make sure you are using the subclass!!
Do it like this:
Just to add to a great @dezinezync answer:
Swift 3+
As dezinezync answered, what you need is to dispatch to the main queue a block of code after
reloadData
from aUITableView
orUICollectionView
, and then this block will be executed after cells dequeuingIn order to make this more straight when using, I would use an extension like this:
It can be also implemented to a
UITableView
as wellI needed some action to be done on all of the visible cells when the collection view get loaded before it is visible to the user, I used:
Pay attention that this will be called when scrolling through the collection view, so to prevent this overhead, I added:
and in the action itself:
The action will still be performed multiple times, as the number of visible cells at the initial state. but on all of those calls, you will have the same number of visible cells (all of them). And the boolean flag will prevent it from running again after the user started interacting with the collection view.