2014-11-20 51 views
4

我有一個方法調用UICollectionViewCell中的下面的代碼。該單元對UICollectionView和基於它的數組都有引用。下面的代碼失敗,出現錯誤:從單元中刪除一個UICollectionViewCell

2014-11-20 13:41:08.976 MyApp[954:304515] *** Assertion failure in -[UICollectionView _endItemAnimations], /SourceCache/UIKit/UIKit-3318.16.14/UICollectionView.m:3917 
2014-11-20 13:41:08.978 MyApp[954:304515] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (1) must be equal to the number of items contained in that section before the update (1), plus or minus the number of items inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).' 
*** First throw call stack: 
(0x22f09d67 0x30768c77 0x22f09c3d 0x23bdcaa9 0x2656863d 0xe8e50 0xe8fa8 0x2680bcc1 0x23c10dc9 0x22ed0517 0x22ed0099 0x22ece303 0x22e1b981 0x22e1b793 0x2a1f4051 0x2640d981 0x1138f4 0x113930 0x30d04aaf) 
libc++abi.dylib: terminating with uncaught exception of type NSException 

守則

//Resolves cell deletion 
    func deleteMe() 
      var path = collectionView.indexPathForCell(self)! 
      collectionArray.removeAtIndex(path.row) 
      collectionView.deleteItemsAtIndexPaths([path]) 
    } 

如何刪除這個小區?

回答

5

在這種情況下,當分配collectionArray或collectionView(我沒有測試看看哪個,但我的賭注是在collectionArray上),它不是一個inout引用,而是一個副本。對於性能問題,Array有一些奇怪的功能。如果我傳遞了對整個父視圖(serviceView)的引用並使用

serviceView.collectionArray.removeAtIndex(path.row) 
serviceView.collectionView.deleteItemsAtIndexPaths([path]) 

它可以正常工作。數據源和顯示的單元格不匹配,因爲該數組沒有被編輯,副本是。