我已经梳理了类似的问题,但这些建议对我没有用。激活编辑模式后,红色的删除图标会出现在左侧,但是无论单击什么图标,都会在右侧显示滑动的删除图标,我必须单击该图标才能触发代码。我想通过单击删除图标直接触发。
@objc func handleEditing() {
if tableView.isEditing == true {
tableView.setEditing(false, animated: true)
navigationItem.leftBarButtonItem?.title = "Edit"
} else {
tableView.setEditing(true, animated: true)
navigationItem.leftBarButtonItem?.title = "Done"
}
}
extension EventsController {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return events.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! EventCell
let event = events[indexPath.row]
cell.textLabel?.text = event.value(forKey: "name") as? String
cell.detailTextLabel?.text = event.value(forKey: "detail") as? String
if let dueDate = event.value(forKey: "isDue") as? Date {
cell.timeLabel.text = returnRemainingTime(isDue: dueDate)
}
return cell
}
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
if tableView.isEditing == true {
return UITableViewCellEditingStyle.delete
}
return .none
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let uid = events[indexPath.row].value(forKey: "uid") as? String
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
for event in events {
if event.value(forKey: "uid") as? String == uid {
context.delete(event)
}
}
do {
try context.save()
retrieveEvents()
} catch let error as NSError {
print("Error saving context, \(error)")
}
}
}
}
超级沮丧,谢谢您的帮助。