【问题标题】:Delete a UICollectionViewCell from the cell从单元格中删除 UICollectionViewCell
【发布时间】:2014-11-20 19:42:31
【问题描述】:

我有一个在 UICollectionViewCell 中调用以下代码的方法。该单元格具有对 UICollectionView 和它所基于的数组的引用。以下代码失败并出现错误:

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])
    }

如何删除此单元格?

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewcell


    【解决方案1】:

    在这种情况下,当分配collectionArray 或collectionView(我没有测试看哪个,但我赌的是collectionArray)时,它不是inout 引用,而是一个副本。 Array 有一些类似这样的奇怪功能来解决性能问题。如果我传入对整个父视图 (serviceView) 的引用并使用

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

    它工作正常。数据源和显示的单元格不匹配,因为数组没有被编辑,一个副本被编辑了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-24
      • 1970-01-01
      • 2017-05-11
      • 1970-01-01
      相关资源
      最近更新 更多