I have a tableview controller that is the master within a split view controller using diffable data sources and I'm trying to figure out an issue I have with it not functioning with swipe to delete options. At first I tried using this series of code:
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let context = fetchedResultsController.managedObjectContext
context.delete(fetchedResultsController.object(at: indexPath))
tableView.deleteRows(at: [indexPath], with: .fade)
do {
try context.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
But that yielded no result, with swiping actually moving from the master to the detail instead. So I next tried using:
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .delete
}
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { (contextualAction, view, completionHandler) in
guard let item = self.dataSource?.itemIdentifier(for: indexPath) else { return }
self.container.viewContext.delete(item)
self.setupSnapshot()
completionHandler(true)
}
deleteAction.image = UIImage(systemName: "trash.fill")
return UISwipeActionsConfiguration(actions: [deleteAction])
}
Again with the same result, no matter what I try the swipe just produces moving to the detail instead. I do get an error when moving to the master in my app that states:
Warning once only: UITableView was told to layout its visible cells and other contents without being in the view hierarchy (the table view or one of its superviews has not been added to a window). This may cause bugs by forcing views inside the table view to load and perform layout without accurate information (e.g. table view bounds, trait collection, layout margins, safe area insets, etc), and will also cause unnecessary performance overhead due to extra layout passes.
I'm not sure if that is impacting what I'm doing or not or what the fix for this would be. Could that be the problem? How do I take care of it? The app still runs but with no function of swipe to delete. I'm really scratching my head here.