UILongPressGestureRecognizer不会从CollectionViewCell实例中取消分配

时间:2014-11-30 21:35:01

标签: ios swift

好的,所以我没有想法

这是我的自定义UICollectionViewCell类:

class baseCViewCell: UICollectionViewCell {

    var mainCV : AnyObject!
    var indexPath : NSIndexPath!

    class func getIdentifier() -> String {
        return NSStringFromClass(self).componentsSeparatedByString(".").last!
    }

    var editGesture : UILongPressGestureRecognizer!

    func initialize(parent:AnyObject, indexPath:NSIndexPath) {
        mainCV = parent as baseCView
        self.indexPath = indexPath

        editGesture = UILongPressGestureRecognizer(target: self, action: Selector("edit:"))
        editGesture.minimumPressDuration = 1.0
        editGesture.allowableMovement = 100.0
        self.addGestureRecognizer(editGesture)

    }

    func edit(gesture: UILongPressGestureRecognizer) {
        if (gesture == editGesture) {
            if (gesture.state == UIGestureRecognizerState.Began) {
                testinglog("Edit pressed on (\(indexPath.row) in \(indexPath.section))")
            }
        }
    }

    deinit {
        self.removeGestureRecognizer(editGesture)
    }

}

我的问题是..为什么在单元格变得不可见(滚动)后手势对象不会被释放。

我已经尝试了所有我知道的强迫他们取消成功......但没有成功

这就是我使用单元格的方式:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 100;
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        var cell = collectionView.dequeueReusableCellWithReuseIdentifier("CViewCell", forIndexPath: indexPath) as CViewCell
        cell.initialize(collectionView, indexPath: indexPath)
        return cell
    }

再次..如果我向上和向下滚动,我可以在仪器中看到内存数量上升......然后上升......只有......等同于泄漏......只是没有检测到。

我评论了添加gestureRecognizer的部分,一切正常(刚才提到它,所以如果我确定问题是否存在,我就不必回答)。

1 个答案:

答案 0 :(得分:1)

集合视图重用单元格。当视图移出屏幕时,它们将从视图中移除并放置在重用队列中,而不是被删除。因此,在取消分配集合视图之前,可能不会调用单元的deinitializer。

您现在正在做的是每次出列时都向单元格添加一个新的手势识别器,这会增加内存使用量。您应该在单元格的初始值设定项中创建并初始化手势识别器。

顺便说一下,你的单元格拥有一个强大的引用回到集合视图,这会创建一个强大的参考周期,这不是一件好事。

相关问题