通过cell中的元素获取cell的indexPath以及长按tableview获取选中cell的indexPath

前端之家收集整理的这篇文章主要介绍了通过cell中的元素获取cell的indexPath以及长按tableview获取选中cell的indexPath前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Swift 3.0 XCode 8.0

通过点击cell中的按钮获取cell的indexPath

如,点击cell中的按钮,删除获取修改等操作。

  1. // 注意层次关系
  2. let cell = btn.superview as! UITableViewCell
  3. // 通过cell本身获取cell的indexPath
  4. let indexpath = self.tv.indexPath(for: cell)

长按tableviewCell后,后去cell的indexPath

用于长按,弹出操作菜单

  1. override func viewDidLoad() {
  2. super.viewDidLoad()
  3.  
  4. let longpress = UILongPressGestureRecognizer(target: self,action: #selector(longPress(gesture:)))
  5. longpress.minimumPressDuration = 0.8
  6. // tableview tv
  7. self.tv.addGestureRecognizer(longpress)
  8. }
  9.  
  10. func longPress(gesture:UILongPressGestureRecognizer) {
  11. let p = gesture.location(in: self.tv)
  12. if gesture.state == .began {
  13. let indexpath = self.tv.indexPathForRow(at: p)
  14. print(indexpath)
  15. }
  16. }

猜你在找的Swift相关文章