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)
}