1

I'm trying to create a way to select a cell so that I can edit it. With the current code, the cell that is going up is always the next cell. And my last cell in my tableView can not be selected.

@objc func handleLongPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) {
    if longPressGestureRecognizer.state == UIGestureRecognizerState.began {
        let touchPoint = longPressGestureRecognizer.location( in : self.view)
        if let indexPath = tableView.indexPathForRow(at: touchPoint) {
            let context = (UIApplication.shared.delegate as!AppDelegate).persistentContainer.viewContext
            let cell_selected = classements[indexPath.row]
            nomAjouterOutlet.text = cell_selected.nom
            pointAjouterOutlet.text = "\(cell_selected.point)"
            classeAjouterOutlet.text = cell_selected.classe!
            // Context CoreData
            context.delete(cell_selected)
            (UIApplication.shared.delegate as!AppDelegate).saveContext()
            do {
                classements =
                try context.fetch(Tournoi.fetchRequest())
            } catch {
                print("Fetching Failed")
            }
            classementTableView.reloadData()
        }
    }
}
override func viewDidLoad() {
    super.viewDidLoad()
    let longPressGesture: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(ClassementViewController.handleLongPress(_: )))
    longPressGesture.minimumPressDuration = 1.0
    longPressGesture.delegate = self
    self.tableView.addGestureRecognizer(longPressGesture)
}
Kozmotronik
  • 2,080
  • 3
  • 10
  • 25
François
  • 33
  • 5

2 Answers2

1

I found my mistake. Here is the code that works.

Swift 4

override func viewDidLoad() {
    super.viewDidLoad()
    setupLongPressGesture()
}

func setupLongPressGesture() {
    let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongPress))
    longPressGesture.minimumPressDuration = 1.0 // 1 second press
    longPressGesture.delegate = self
    self.tblMessage.addGestureRecognizer(longPressGesture)
}

@objc func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer){
    if gestureRecognizer.state == .ended {
        let touchPoint = gestureRecognizer.location(in: self.tblMessage)
        if let indexPath = tblMessage.indexPathForRow(at: touchPoint) {

        }
    }
}

From shareedit answered Jan 18 at 11:05

PinkeshGjr 1,28121225

HangarRash
  • 7,314
  • 5
  • 5
  • 32
François
  • 33
  • 5
0

Since the OP's response does not have enough explanation I wanted to add this answer as a complementary answer for the OP's answer. The mistake was actually in the following piece of code:

let touchPoint = longPressGestureRecognizer.location( in : self.view)

Since the location is obtained from within the main view, the touch point returned is relative to view. However there is a difference between he y points of view and tblMessage which causing a shift to the below of the selected cell. This may be due to a navigation bar within the view that causing this shift in y point. If the height of the navigation ba is known we could get the correct touch point by applying the difference to the touchPoint's y property such as touchpoint.y -= navBarHeight.
So the appropriate way of getting the touch point would be using the tableView as an offset so that the location(in:) function can calculate the touch point correctly. Eventually, when we replace the location(in:)'s parameter from view to tblMessage which is tableView itself, we should get the correct point.

let touchPoint = gestureRecognizer.location(in: self.tblMessage)
Kozmotronik
  • 2,080
  • 3
  • 10
  • 25