I have a table that uses a coredata fetch to populate its self, the fetch controller stores the delete row action.
However I want to add an edit feature to the row as well, I dont seem to be able to add this along side the delete feature I currently have and need to use editActionsForRowAt to get it to appear.
When i use editActionsForRowAt it overrides by existing Fetch Controllers row actions and causes the delete to no longer work.
Any idea how I can add just an edit next to the existing delete without over writing or messing with the fetch results controller?
Here is the code for the fetch controller that is the source for the table and its delete case and delete func, I want to ideally add an edit button to this, without having to overwrite it all with editActionsForRowAt
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch (type) {
case .insert:
if let indexPath = newIndexPath {
workoutDesignerTable.insertRows(at: [indexPath], with: .fade)
}
break;
case .delete:
if let indexPath = indexPath {
workoutDesignerTable.deleteRows(at: [indexPath], with: .fade)
}
break;
case .update:
if let indexPath = indexPath, let cell = workoutDesignerTable.cellForRow(at: indexPath) as? RoutineTableViewCell {
configure(cell, at: indexPath)
}
break;
default:
print("...")
}
}
// MARK: - DELETING TABLE ROW
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let UserExercise = fetchedResultsController.managedObjectContext
UserExercise.delete(self.fetchedResultsController.object(at: indexPath))
do {
try UserExercise.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
For more information please find the below screenshot of current Table Row Edit Actions.

From the documentation tableView(_:editActionsForRowAt:)
Use this method when you want to provide custom actions for one of your table rows. When the user swipes horizontally in a row, the table view moves the row content aside to reveal your actions. Tapping one of the action buttons executes the handler block stored with the action object.
If you do not implement this method, the table view displays the standard accessory buttons when the user swipes the row.
So you need to also pass the delete action button with your other buttons.
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let btnEdit = UITableViewRowAction(style: .default, title: "Edit") { action, indexPath in
//Put the code of edit action
}
let btnDelete = UITableViewRowAction(style: .destructive, title: "Delete") { action, indexPath in
//Put the code of delete action
}
return [btnEdit, btnDelete]
}
Note: If you are implementing tableView(_:editActionsForRowAt:) then there is no need to implement tableView(_:commit:forRowAt:) method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With