Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongPress(longPressGesture:)))
  2. longPressGesture.minimumPressDuration = 1.0 // 1 second press
  3. longPressGesture.delegate = self
  4. self.tblListView.addGestureRecognizer(longPressGesture)
  5.  
  6. @objc func handleLongPress(longPressGesture:UILongPressGestureRecognizer) {
  7.  
  8. let p = longPressGesture.location(in: self.tblListView)
  9. let indexPath = self.tblListView.indexPathForRow(at: p)
  10.  
  11. if indexPath == nil {
  12. print("Long press on table view, not row.")
  13. }
  14. else if (longPressGesture.state == UIGestureRecognizer.State.began) {
  15. print("Long press on row, at (indexPath!.row)")
  16. let cell = self.tblListView.cellForRow(at: indexPath!) as! GroupDetailTableViewCell
  17. cell.btnDeleteMember.isHidden = false
  18. }
  19. }
  20.  
  21. I think you can want to hide or unhide btnDeleteMember.If so use the following code :
  22.  
  23. @objc func handleLongPress(longPressGesture:UILongPressGestureRecognizer) {
  24.  
  25. let p = longPressGesture.location(in: self.tblListView)
  26. let indexPath = self.tblListView.indexPathForRow(at: p)
  27.  
  28. if indexPath == nil {
  29. print("Long press on table view, not row.")
  30. }
  31. else if (longPressGesture.state == UIGestureRecognizer.State.began) {
  32. print("Long press on row, at (indexPath!.row)")
  33. let cell = self.tblListView.cellForRow(at: indexPath!) as! GroupDetailTableViewCell
  34. cell.btnDeleteMember.isHidden = !cell.btnDeleteMember.isHidden
  35. }
  36.  
  37. }
  38.  
  39. if let selectedIndexPath = self.tblListView.indexPathForSelectedRow {
  40. self.tblListView.deselectRowAtIndexPath(at: selectedIndexPath, animated: true)
  41. }
  42.  
  43. // Global variable
  44. var previousIndexPath : IndexPath = IndexPath()
  45.  
  46. @objc func handleLongPress(longPressGesture:UILongPressGestureRecognizer) {
  47.  
  48. let p = longPressGesture.location(in: self.tblListView)
  49. let indexPath = self.tblListView.indexPathForRow(at: p)
  50.  
  51. if indexPath == nil {
  52. print("Long press on table view, not row.")
  53. }
  54. else if (longPressGesture.state == UIGestureRecognizer.State.began) {
  55. print("Long press on row, at (indexPath!.row)")
  56.  
  57. let cell = self.tblListView.cellForRow(at: indexPath!) as! GroupDetailTableViewCell
  58. cell.btnDeleteMember.isHidden = previousIndexPath == indexPath ?
  59. true : false // This will make the Select and Deselect
  60. previousIndexPath = indexPath
  61. }
  62. }
  63.  
  64. }
  65.  
  66. var selectedIndex:IndexPath?
  67.  
  68. else if (longPressGesture.state == UIGestureRecognizer.State.began) {
  69. print("Long press on row, at (indexPath!.row)")
  70. selectedIndex = selectedIndex == indexPath ? nil : indexPath
  71. self.tableView.reloadData()
  72. }
  73.  
  74. if let current = selectedIndex , current.row == indexPath.row {
  75. // selected
  76. }
  77. else {
  78. // not selected
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement