**Invalid update: invalid number of items in section 0 **
在调用 insertItems 函数时 需要注意 如果当前的weakSelf.collectionView为空, 那么直接使用 reloadData
如果当前collectionView不为空 才使用insertItems
open func performBatchUpdates(_ updates: (() -> Void)?, completion: ((Bool) -> Void)? = nil)
DispatchQueue.main.async {[weak self] in
guard let weakSelf = self else { return}
guard weakSelf.dataSources.count > 1 else {
weakSelf.collectionView?.reloadData();
return
}
weakSelf.collectionView?.performBatchUpdates({[weak self] in
var insters = [IndexPath]()
let showNumber = weakSelf.collectionView?.numberOfItems(inSection:0) ?? 0
for index in showNumber ..< weakSelf.dataSources.value.count {
insters.append(contentsOf: [IndexPath.init(row:index, section:0)])
}
self?.collectionView?.insertItems(at:insters)
}, completion: { ( _ ) in
self?.collectionView?.collectionViewLayout.invalidateLayout()
self?.collectionView?.collectionViewLayout.finalizeCollectionViewUpdates()
})
}