我正在尝试创建一种选择单元格的方法,以便我可以对其进行编辑。使用当前代码,正在上升的单元始终是下一个单元。我的tableView
中的最后一个单元格无法选中。
@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)
}
答案 0 :(得分:0)
我发现了我的错误,这是有效的代码!谢谢分享
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) {
}
}
}
shareedit 1月18日11:05回答。
PinkeshGjr 1,28121225