2017-09-10 3 views
0

内の項目の無効な数が、最近、私は次のエラーました:無効更新:セクション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).

をエラーが私の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) 
      } 
    }) 

私だけです私のコレクションビュー内の1つのセクション使用:

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

を私は、同じトピックに関する記事のカップルをチェックアウトしますが、していますyは私の推測では、問題は、次の2つのラインであるということです、私の問題を解決していないが、私はわからない:

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

を助けてください。

答えて

1

insertItems(at:)deleteItems(at:)の呼び出しには、データソースの変更も伴わなければなりません。だから、

、これらのAPIを呼び出す前に、あなたはすなわちinsertItemsを呼び出す前に、それにオブジェクトを追加し、deleteItems

を呼び出す前に、そこからオブジェクトを削除し、あなたのデータソースを変更したいです
関連する問題