2016-02-15 90 views
0

我想從UICollectionView中刪除單元格。雖然刪除單元格,我得到NSInternalInconsistencyException從collectionView中刪除單元格

***終止應用程序由於未捕獲的異常「NSInternalInconsistencyException」,理由是:「無效的更新:在第0包含在現有的部分更新後的項目數項目數無效(5)必須等於更新之前該部分中包含的項目數(5),加上或減去從該部分插入或刪除的項目數(插入0個,刪除1個),加上或減去項目數移入或移出該部分(0移入,0移出)。'錯誤。

這裏是我的代碼:

[self.imgArray removeObjectAtIndex:indexForDelete]; 

[self.collectionView performBatchUpdates:^{ 

    NSIndexPath *indexPath =[NSIndexPath indexPathForItem:indexForDelete inSection:0]; 
    [self.collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]]; 
    } completion:^(BOOL finished) { 

}]; 

我追加一個虛擬池時,我的數組數不爲5 這裏是numberOfItems代碼numberOfItemsInSection

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    if(self.imgArray.count == 5) 
    { 
     return self.imgArray.count ; 
    } 
    else 
    { 
     return self.imgArray.count + 1; 
    } 

} 

我發現很多的解決方案谷歌和Stackoverflow,但沒有發現幫助完整的我。

+0

你並不需要一個單一的刪除'performBatchUpdates'。擺脫這一點,只是保持塊內的兩條線。 – Paulw11

+0

你可以顯示你的'numberOfItemsInSection'方法嗎? – Paulw11

+0

看到我更新的問題。 @ Paulw11 –

回答

0

performBatchUpdates主要用於執行多個單元格的操作。即刪除,插入,移動。

試試這個,

[self.collectionObj performBatchUpdates:^{ 

    [self.imgArray removeObjectAtIndex:indexForDelete]; 

    NSIndexPath *indexPath =[NSIndexPath indexPathForItem:indexForDelete inSection:0]; 
    [self.collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]]; 

    [self.collectionView reloadData]; 

} completion:^(BOOL finished) { 

}]; 

您也可以直接嘗試1個單元刪除不使用performBatchUpdates

[self.imgArray removeObjectAtIndex:indexForDelete]; 

    NSIndexPath *indexPath =[NSIndexPath indexPathForItem:indexForDelete inSection:0]; 
    [self.collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]]; 

    [self.collectionView reloadData]; 

希望,這將幫助你。
謝謝

+0

雖然這會解決問題,但我建議您添加一些註釋來解釋爲什麼更新'performBatchUpdates'中的數組是必要的 – Paulw11

+0

嘗試不起作用。 (Y) –

+0

@ Paulw11是的。圖片= [UIImage imageNamed:@「add_Gallary」]; } else { // cell item from imgArray; } –

0

我想你可能需要將更新分成兩個階段以避免NSInternalInconsistencyException。

@property (assign, nonatomic) BOOL isMyCollectionViewUpdating; 

//... 

[self.imgArray removeObjectAtIndex:indexForDelete]; 

[self.collectionView performBatchUpdates:^{ 

    self.isMyCollectionViewUpdating = YES; 

    NSIndexPath *indexPath =[NSIndexPath indexPathForItem:indexForDelete inSection:0]; 
    [self.collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]]; 
} completion:^(BOOL finished) { 
     self.isMyCollectionViewUpdating = NO; 
     [self.collectionView reloadData]; 
}]; 

和集合視圖numberOfItemInSection

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
    { 
     if (self.isMyCollectionViewUpdating) 
     { 
      return self.imgArray.count; 
     } else { 
      if(self.imgArray.count == 5) 
      { 
       return self.imgArray.count ; 
      } 
      else 
      { 
       return self.imgArray.count + 1; 
      } 
     } 


    }