我有一个集合视图,我在每个呼叫中加载20个单元格(取)从火药桶里。因此,我有两个部分,第一部分是我的源数组的计数号,第二个部分只是显示加载单元格时,bool价值fetch more是真的。
这是集合视图代码:
extension ProductViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
return self.objectArray.count
} else if section == 1 {
return fetchingMore ? 1 : 0
}
return 0
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
cellHeights[indexPath] = cell.frame.size.height
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
var minimumLineSpacingForSectionAt: CGFloat
if section == 0 {
minimumLineSpacingForSectionAt = -50
} else {
minimumLineSpacingForSectionAt = 0
}
return minimumLineSpacingForSectionAt
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if indexPath.section == 0 {
return CGSize(width: productCollectionView.bounds.width, height: CommonService.heightForRowAtIndexPathForiPhones(cellType: .itemCell))
} else {
return CGSize(width: productCollectionView.bounds.width, height: 70)
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.section == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ItemAlbumCollectionViewCell", for: indexPath) as! ItemAlbumCollectionViewCell
cell.setUpCell(product: objectArray[indexPath.row]!)
return cell
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LoadingCollectionViewCell", for: indexPath) as! LoadingCollectionViewCell
cell.activityIndicator.startAnimating()
return cell
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let productCategoryViewController = Storyboards.ProductCategoryVC.controller as! ProductCategoryViewController
productCategoryViewController.products = objectArray[indexPath.row]!
pushViewController(T: productCategoryViewController)
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionView.elementKindSectionHeader:
guard let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "StreachyCollectionHeaderReusableView", for: indexPath) as? StreachyCollectionHeaderReusableView else { fatalError("Invalid view type") }
return headerView
default:
assert(false, "Invalid element type")
}
}
}
这是为获取数据而调用的函数:
func pagination(forDivisionCollection: String) {
fetchProduct(forDivisionCollection: forDivisionCollection, completion: { newData in
self.productCollectionView.reloadSections(IndexSet(integer: 1))
self.objectArray.append(contentsOf: newData)
self.endReached = newData.count == 0
self.fetchingMore = false
UIView.performWithoutAnimation {
self.productCollectionView.reloadData()
}
})
}
而且它运行得很完美。
现在,我添加了一个顶部菜单部分,有几个选项。 https://i.stack.imgur.com/bMAIr.png 如果用户点击该部分,则函数func pagination(forDivisionCollection: String)将使用参数调用数据源,并使用该参数筛选数据源,并根据顶部菜单选项填充数据源,如下所示:
func pagination(isForNewDivision: Bool, forDivisionCollection: String) {
self.fetchingMore = true
if isForNewDivision == true {
self.objectArray.removeAll()
}
fetchProduct(forDivisionCollection: forDivisionCollection, completion: { newData in
self.productCollectionView.reloadSections(IndexSet(integer: 1))
self.objectArray.append(contentsOf: newData)
self.endReached = newData.count == 0
self.fetchingMore = false
UIView.performWithoutAnimation {
self.productCollectionView.reloadData()
}
})
}
但我得到了'Invalid update: invalid number of items in section 0.在模拟器中运行Xcode并在物理设备上崩溃时,对Xcode发出警告。我对此做了一些研究,并提出了使用的解决方案。performBatchUpdates但他们都不起作用。因此,我的问题是,在这种情况下,在收集视图中获取数据和加载数据的最佳方法是什么?提前谢谢。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。