Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to swipe to delete with tableview using diffable data source in iOS 13

I'm updating a UITableViewController to use the new UITableViewDiffableDataSource, I have everything working except Swipe to delete.

This is an example of how I use swipe to delete

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {     let lockedAction = UIContextualAction(style: .normal, title: "TEST") { (_, _, completion) in        print("tapped....")        completion(true)    }      return UISwipeActionsConfiguration(actions: [lockedAction]) } 

But this doesn't not work in a UITableViewController that has UITableViewDiffableDataSource

There is no swipe, a break point within the method is never called either

I thought this was a beta bug, but I updated to Xcode 11 GM and that same thing is occurring.

Thanks for any advice

like image 360
DogCoffee Avatar asked Sep 11 '19 23:09

DogCoffee


People also ask

How do I swipe to delete Uitableviewcells?

When you want to handle deleting, you have to do three things: first, check that it's a delete that's happening and not an insert (this is down to how you use the UI); second, delete the item from the data source you used to build the table; and third, call deleteRows(at:) on your table view.

What is Diffable data source?

A diffable data source object is a specialized type of data source that works together with your table view object. It provides the behavior you need to manage updates to your table view's data and UI in a simple, efficient way.


1 Answers

It's true that the docs for tableView(_:canEditRowAt:) say:

The method permits the data source to exclude individual rows from being treated as editable. Editable rows display the insertion or deletion control in their cells. If this method is not implemented, all rows are assumed to be editable

However UITableViewDiffableDataSource, does implement that method and it seems to return false by default (though I can't find that documented anywhere).

In fact the sample code from WWDC 2019 sessions 215 and 220 implements a custom UITableViewDiffableDataSource subclass like this:

class DataSource: UITableViewDiffableDataSource<SectionType, ItemType> {     // ...      override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {         return true     }     // ... } 
like image 60
Alex Robinson Avatar answered Sep 25 '22 01:09

Alex Robinson