作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 collectionView,它有 3 个部分,每个部分都有一个不同的单元格(将单元格的数据分成单独的部分比只分成 1 个部分更容易)。
collectionView 的数据源中只有一个 dataModel,它会更新,但我只需要在第一部分显示该更新(添加了一个新标签)。当我尝试运行 collectionView.performBatchUpdates
时,整个布局变得困惑,从显示所有 3 个部分到只显示最后一个部分,前两个完全消失。这不是约束,因为当我弹出并推回布局时,新标签是完美的,它在第一部分显示更新的数据,而其他 2 个看起来很好。问题似乎发生在我运行 perform batchUpdates 时
我试过:
let updatedDataModel = ...
guard let indexOfItem = tableData.firstIndex(where: { $0.itemId == updatedDataModel.itemId }) else { return }
tableData[indexOfItem] = updatedDataModel
let indexPath = IndexPath(item: indexOfItem, section: 0)
UIView.animate(withDuration: 0) {
self.collectionView.performBatchUpdates({ [weak self] in
self?.collectionView.reloadItems(at: [indexPath])
self?.collectionView.layoutIfNeeded()
}) { (_) in
print("success")
}
}
我试过:
collectionView.performBatchUpdates({
let indexSet = IndexSet(integer: 0)
self.collectionView.reloadSections(indexSet)
}, completion: nil)
我试过:
let updatedDataModel = ...
guard let indexOfItem = tableData.firstIndex(where: { $0.itemId == updatedDataModel.itemId }) else { return }
tableData[indexOfItem] = updatedDataModel
let indexPath0 = IndexPath(item: indexOfItem, section: 0)
let indexPath1 = IndexPath(item: indexOfItem, section: 1)
let indexPath2 = IndexPath(item: indexOfItem, section: 2)
UIView.animate(withDuration: 0) {
self.collectionView.performBatchUpdates({ [weak self] in
self?.collectionView.reloadItems(at: [indexPath0, indexPath1, indexPath2])
self?.collectionView.layoutIfNeeded()
}) { (_) in
print("success")
}
}
单元格项目:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// *** this section is where the updated data (an additional label) needs to appear
if indexPath.section == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: topCell, for: indexPath) as! TopCell
cell.dataModel = tableData[indexPath.item]
return cell
}
if indexPath.section == 1 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: middleCell, for: indexPath) as! MiddleCell
cell.dataModel = tableData[indexPath.item]
return cell
}
if indexPath.section == 2 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: bottomCell, for: indexPath) as! BottomCell
cell.dataModel = tableData[indexPath.item]
return cell
}
// if for some reason something goes wrong above but this NEVER runs
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: errorCell, for: indexPath) as! ErrorCell
return errorCell
}
数据源:
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
return 1
}
if section == 1 {
return tableData.count
}
if section == 2 {
return 1
}
return 0
}
我哪里出错了?
最佳答案
不要使用collectionView.performBatchUpdates
,而只需编写以下内容以仅在您想要的部分重新加载数据:
let indexSet = IndexSet(integer: 0) // Change integer to whatever section you want to reload
collectionView.reloadSections(indexSet)
如果您不希望此 UICollectionView
重新加载任何动画,请使用:
UIView.performWithoutAnimation {
// same code as above
}
关于ios - Swift - 如何在具有多个部分的 collectionView 上执行 BatchUpdates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57007461/
我是一名优秀的程序员,十分优秀!