Swift 3.0 XCode 8.0
通过点击cell中的按钮获取cell的indexPath
// 注意层次关系
let cell = btn.superview as! UITableViewCell
// 通过cell本身获取cell的indexPath
let indexpath = self.tv.indexPath(for: cell)
长按tableviewCell后,后去cell的indexPath
用于长按,弹出操作菜单。
override func viewDidLoad() {
super.viewDidLoad()
let longpress = UILongPressGestureRecognizer(target: self,action: #selector(longPress(gesture:)))
longpress.minimumPressDuration = 0.8
// tableview tv
self.tv.addGestureRecognizer(longpress)
}
func longPress(gesture:UILongPressGestureRecognizer) {
let p = gesture.location(in: self.tv)
if gesture.state == .began {
let indexpath = self.tv.indexPathForRow(at: p)
print(indexpath)
}
}