0

我遇到了很大的麻烦,因为当我在我的单元格中选择一个单元格collectionView并更改我的数组数据时,我收到了以下错误消息:

'无效更新:第0节中的项目数无效。更新后现有节中包含的项目数(2)必须等于更新前该节中包含的项目数(2),加上或减去从该部分插入或删除的项目数(1 插入,0 删除)加上或减去移入或移出该部分的项目数(0 移入,0 移出)。

它发生在 iOS 13 之前。在 iOS 13 和更高版本中运行良好。有我的代码:

import UIKit
import ReactiveKit
import Bond

//Its in my viewModel

var arrayOfObjects = MutableObservableArray<MyObject>([])


  actionCollectionViewDidSelect.observeNext { [weak self] indexPath in
        guard indexPath.row != 0 else { return }
        guard let object = self?.arrayOfPhotoObjects[indexPath.row] else { return }
        photoObject.isSelected = !photoObject.isSelected
        if let value = self?.arrayOfObjects.value { self?.arrayOfPhotoObjects.value = value } // Gives error because there but I can't find solution.
    }.dispose(in: disposeBag)

//Its in my ViewController


collectionView.reactive.selectedItemIndexPath.observe(with: viewModel.actionCollectionViewDidSelect).dispose(in: disposeBag)

    viewModel.arrayOfObjects.bind(to: collectionView) { object, indexPath, collectionView in
        
        let objectForIndex = arrayObjects[indexPath.row]
       
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "photoBoxCollectionViewCell", for: indexPath) as! MYCEll
             cell.data = objectForIndex
            return cell
        }
    }
4

1 回答 1

0

当我尝试更改阵列时,我发现了问题,它遇到了麻烦。我的解决方案是:

  actionCollectionViewDidSelect.observeNext { [weak self] indexPath in
    guard indexPath.row != 0 else { return }
    guard let object = self?.arrayOfPhotoObjects[indexPath.row] else { return }
    photoObject.isSelected = !photoObject.isSelected
    if let value = self?.arrayOfObjects.value {                     
            let collection = value.collection
            let newArrayObject = MutableObservableArray<PhotoObject(collection)
            self?.arrayOfPhotoObjects.value = newArrayObject.value }
   }.dispose(in: disposeBag)
于 2021-01-04T21:05:51.267 回答