gpt4 book ai didi

ios - Swift - 如何在具有多个部分的 collectionView 上执行 BatchUpdates

转载 作者:行者123 更新时间:2023-11-28 13:33:33 24 4
gpt4 key购买 nike

我有一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com