2015-07-21 307 views
2

我注意到的方法在iOS7(壞 - 崩潰)和iOS8(好)中的工作方式有一個奇怪的差異。下面的代碼我使用:UICollectionView的performBatchUpdates崩潰iOS7

[self.swapItems removeObject:self.swapItems[indexPath.row]]; 

[self.swapItemsGrid performBatchUpdates:^{ 
    [self.swapItemsGrid deleteItemsAtIndexPaths:@[indexPath]]; 
} completion:^(BOOL finished) { 
    [self layoutViews]; 
}]; 

在iOS8上能正常工作,而在iOS7它與下面的錯誤崩潰:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]' 

一點點調試表明,在iOS8上的performBatchUpdates:completion:方法調用數據源方法collectionView:numberOfItemsInSection:,而在iOS7中它不會,因此,嘗試用不在數據數組中的對象的數據創建單元格的錯誤。

有沒有其他人遇到過這個問題?也許你有一個解決方案?

+0

哦,值得注意的是,只有當我刪除一個項目(執行上面的代碼 - 沒有崩潰),添加一個項目,再次刪除一個項目(執行上面的代碼 - CRASH)時纔會發生。在這兩種情況下,'self.swapItems'數組都是相同的。 – artooras

+0

我有這個相同的問題。你有沒有想出一個解決方案? – Snowman

+0

不幸的是,還沒有。 – artooras

回答

0

self.swapItemsGrid可能沒有物品在索引indexPath。 爲了避免這種情況,使用此檢查indexPath出口在陣列與否:

-(BOOL)checkIndexPathExits:(NSIndexPath*)indexPath inArray:(NSArray*)array{ 
    if (indexPath.section < array.count) { 
     if (indexPath.row < [array[indexPath.section] count]) { 
      return YES; 
     } 
    } 
    return NO; 
} 

希望這可以幫助。

+0

嗨。恐怕不是那麼容易。我唯一可以做這種檢查的地方是'collectionView:cellForItemAtIndexPath:'方法,如果方法被調用,我必須返回一個'UICollectionViewCell'的實例。這就是爲什麼有'collectionView:numberOfItemsInSection:'方法來確保數據數組對每個請求的'indexPath'有足夠的對象。 – artooras

相關問題