Reloading a UICollectionView using reloadData method returns immediately before reloading data

前端 未结 3 1210
北海茫月
北海茫月 2020-12-07 19:34

I need to know when reloading a UICollectionView has completed in order to configure cells afterwards (because I am not the data source for the cells - other wise would have

3条回答
  •  萌比男神i
    2020-12-07 19:49

    If you'd like to perform some code after your collectionView has completed it's reloadData() method, then try this (Swift):

        self.collectionView.reloadData()
        self.collectionView.layoutIfNeeded()
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            // Put the code you want to execute when reloadData is complete in here
        }
    

    The reason this works is because the code within the dispatch block gets put to the back of line (also known as a queue). This means that it is waiting in line for all the main thread operations to finish, including reloadData()'s methods, before it becomes it's turn on the main thread.

提交回复
热议问题