//the viewcontroller is initiated with UIGestureRecognizerDelegate
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
It looks like you are trying to make longPressGesture
an instance property of your UIViewController while giving it a target and action as part of its initializer. That's not going to work, because at the time it is initialized, the target, self
, is not the instance. There is no instance yet; the instance is what we are in the middle of creating!
Instead, move that line into cellForRowAt:
, like this:
//in cellForRowAt:
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
longPressGesture.minimumPressDuration = 1.0
longPressGesture.delegate = self
longPressGesture.cancelsTouchesInView = false
cell.addGestureRecognizer(longPressGesture)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…