我一直试图在我的uitableview上设置长按手势识别器。我已经能够让它注册,但它似乎给我关联行的不正确的信息。我的tableview按正常点击按预期工作,它传递indexPath.row
,我能够从与该行关联的people
数组中获取正确的记录。但是,在使用下面的代码时,indexPath.row似乎不一致并选择上方和下方的行,然后在滚动时,它会在长按时选择随机记录。
func longPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizerState.began {
let touchPoint = longPressGestureRecognizer.location(in: self.view)
if let indexPath = tableView.indexPathForRow(at: touchPoint) {
let person = people[indexPath.row]
print("\(person.name)")
///////works but erratic responses//////////
}
}
}
//// in view did load i have this
let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(PeopleVC.longPress(_:)))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self
self.tableView.addGestureRecognizer(longPressGesture)
答案 0 :(得分:3)
改变这个:
interface MyRequestTypeAction{
void doSomething();
}
Map<Request, MyRequestTypeAction> requestActions = new HashMap<>(){{
put(TypeAction.TYPE1,()->System.out.printLine("TypeAction.TYPE1"));
put(TypeAction.TYPE2,()->System.out.printLine("TypeAction.TYPE2"));
put(TypeAction.TYPE3,()->System.out.printLine("TypeAction.TYPE3"));
put(OtherTypeAction.OTHERTYPE1,()->System.out.printLine("OtherTypeAction.OTHERTYPE1"));
put(OtherTypeAction.OTHERTYPE2,()->System.out.printLine("OtherTypeAction.OTHERTYPE2"));
}}
requestActions.get(a_request).doSomething();
对此:
let touchPoint = longPressGestureRecognizer.location(in: self.view)
您正在let touchPoint = longPressGestureRecognizer.location(in: self.tableView)
而非UITableView
内寻找手势。