'Is there any way to swipe to delete/ Add actions into cell in UITableview Diffable datasource?
I am trying to add swipe actions into my tableview. when I used simple tableview datasource method it worked fine (trailingSwipeActionsConfigurationForRowAt). but when I tried the same thing with Diffable datasource it didn't even call the method for swiping. I used breakpoints to get followup but it didn't work. I am using swift 5 (UIKit), Xcode 12.4
Solution 1:[1]
I solved this issue by following method: Used subclass of diffabled datasource as follows.
class GrocDataSource: UITableViewDiffableDataSource <GrocListSections, Grocs> {
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
if indexPath.section == 0 || indexPath.section == 2 {
return true
} else {
return false
}
}
}
Implemented this class into datasource as:
datasource = GrocDataSource(tableView: grocTableView, cellProvider: { (tableView, indexPath, grocs) in
guard let cell = tableView.dequeueReusableCell(withIdentifier: "GrocNameCell", for: indexPath) as? GrocNameCell else {return UITableViewCell()}
return cell })
Solution 2:[2]
Wrote a demo and it works fine.
CustomDiffableDataSource.swift
import UIKit
enum Section: CaseIterable {
case main
}
struct City: Hashable {
var name: String
let identifier = UUID()
func contains(query: String?) -> Bool {
guard let query = query else {
return true
}
guard !query.isEmpty else {
return true
}
return name.contains(query)
}
}
class CustomDiffableDataSource: UITableViewDiffableDataSource<Section, City> {
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
true
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
print("delete")
// get item data
guard let item = itemIdentifier(for: indexPath) else {
return
}
print("item: ", item)
}
}
TableViewController.swift
import UIKit
class TableViewController: UITableViewController {
lazy var cities: [City] = {
let cityNames = ["A", "B", "C", "D", "E", "F", "G"]
var cities = [City]()
for name in cityNames {
cities.append(City(name: name))
}
return cities
}()
var dataSource: UITableViewDiffableDataSource<Section, City>!
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
createDataSource()
}
func createDataSource() {
dataSource = CustomDiffableDataSource(tableView: tableView, cellProvider: { tableView, _, city in
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell?.textLabel?.text = city.name
return cell
})
dataSource.defaultRowAnimation = .fade
show()
}
func show() {
var snapshot = dataSource.snapshot()
snapshot.appendSections([.main])
snapshot.appendItems(cities, toSection: .main)
dataSource.apply(snapshot, animatingDifferences: true)
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jatesh Kumar Maheshwari |
Solution 2 |