简体   繁体   中英

PerformBatchUpdates for collection view completion handler not being executed

Interesting problem I am running into currently. When invoking the performBatchUpdates method on an instance of a UICollectionView certain cases cause the completion handler to not be executed.

collectionView.performBatchUpdates({
            collectionView.insertItems(at: indexPaths)
        }) { _ in
            if self.delegate.responds(to: #selector(self.delegate.sectionNeedsToBeHidden)) {
                self.delegate.sectionNeedsToBeHidden?()
            }
        }
    }

When performing an insertion to the collection view the completion handler is not invoked. However, when the same logic is executed except the line collectionView.insertItems(at: indexPaths) is changed to collectionView.deleteItems(at: indexPaths) the completion handler is then invoked. My current guess as to why this is happening is something regarding the previous state of the collection view and it is only registering a change to the data source upon deletion therefore, causing the completion handler to be invoked. That solution however, is not definitive as to why the completionHandler for performBatchUpdates is only being invoked upon the deletion of items and not the insertion of items. If anyone has any insights on this exact problem or in general of how the performBatchUpdates work I would appreciate

Whenever you insert a row or delete an item from collection view you must have to update a number of items data like below code

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.myItems.count
}

self.myItems.count need to update first. So First update your items count then perform batch update.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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