2017-09-10 8 views
0

Récemment, j'obtenu l'erreur suivante:mise à jour invalide: numéro incorrect d'articles dans la section 0.

Fatal Exception: NSInternalInconsistencyException Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (13) must be equal to the number of items contained in that section before the update (12), plus or minus the number of items inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).

L'erreur se produit dans le code suivant dans mon client tvOS:

let removedIndexPaths = removedIndexes.map({ IndexPath(row: $0, section: 0) }) 
let addedIndexPaths = addedIndexes.map({ IndexPath(row: $0, section: 0) }) 
let updatedIndexPaths = updatedIndexes.map({ IndexPath(row: $0, section: 0) }) 

    self.collectionView?.performBatchUpdates({ 
     self.collectionView?.deleteItems(at: removedIndexPaths) 
     self.collectionView?.insertItems(at: addedIndexPaths) 
     }, completion: { _ in 
      guard let collectionView = self.collectionView else { 
       return 
      } 

      for indexPath in updatedIndexPaths { 
       if let myCell = collectionView.cellForItem(at: indexPath) as? MyCollectionViewCell { 
        let item = self.dataManager.items[indexPath.row] 
        myCell.updateUI(item) 
       } 
      } 

      let collectionViewLayout = self.collectionViewLayoutForNumberOfItems(self.dataManager.items.count) 
      if collectionViewLayout.itemSize != self.collectionFlowLayout.itemSize { 
       collectionView.setCollectionViewLayout(collectionViewLayout, animated: false) 
      } 
    }) 

Je ne suis en utilisant une section dans ma collection voir:

override func numberOfSections(in collectionView: UICollectionView) -> Int { 
    return 1 
} 

J'ai vérifié quelques messages sur le même sujet, mais le y n'a pas résolu mon problème, je suppose que le problème est dans les deux lignes suivantes, mais je ne suis pas sûr:

self.collectionView?.deleteItems(at: removedIndexPaths) 
self.collectionView?.insertItems(at: addedIndexPaths) 

S'il vous plaît aider.

Répondre

1

L'appel à insertItems(at:) et deleteItems(at:) doit également être accompagné de la modification de la source de données.

Donc, avant d'appeler ces API, vous voulez changer votre source de données, à savoir ajouter des objets dans le avant d'appeler insertItems et supprimer des objets avant d'appeler deleteItems