So, I can add tasks and persist those tasks in my app. Now what I would like to do is be able to delete tasks and persist what I deleted. I am using the swipeable cell cocoa pod in order to allow the user to swipe to the left and delete items. However, my code below doesn't seem to function the way I want it to. First of, the swipe gesture isn't working and secondly if I had "task1", "task2", "task3", I can't delete task 3 but I can delete task 1 then I can delete task 3. Also, sometimes my deleted item is persisted and sometimes not. My code is below, any help is greatly appreciated.
class DailyTasksTableViewController: UITableViewController {
//Variables
var tasksArray = [Task]()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
.....
extension DailyTasksTableViewController: SwipeTableViewCellDelegate {
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
guard orientation == .right else { return nil }
let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
self.context.delete(self.tasksArray[indexPath.row])
self.tasksArray.remove(at: indexPath.row)
do {
try self.context.save()
}catch{
print("error saving delete \(error)")
}
}
// customize the action appearance
deleteAction.image = UIImage(named: "delete-icon")
return [deleteAction]
}
func tableView(_ tableView: UITableView, editActionsOptionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> SwipeOptions {
var options = SwipeOptions()
options.expansionStyle = .destructive
return options
}
}
The answer for "First of, the swipe gesture isn't working"
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.