有一个使用UITableView的UITableViewDiffableDataSource。我子类UITableViewDiffableDataSource以添加canEditRowAt。这正确地显示了在指向行时可滑动的删除操作。但是,单击delete选项不会调用tableView(_:commit:forRowAt:)。我有朗读,您必须使用tableView(_:trailingSwipeActionsConfigurationForRowAt:indexPath:),因为不支持其他函数。我想确认那是真的。如果我们也子类tableView(_:commit:forRowAt:),我们需要一种干净的方法来调用原始视图控制器上的函数。
// MyViewController
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete cell
}
}
// Subclass DiffableDataSource used in MyViewController
final class CustomDiffableDatasource: UITableViewDiffableDataSource<MyViewController.Section, MyViewController.Item> {
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
guard let item = itemIdentifier(for: indexPath) else {
return false
}
return item.isEditable
}
}发布于 2022-02-25 23:02:26
以下是自定义的trailingSwipeActionsConfigurationForRowAt功能,用于添加删除滑动。我在另一个StackOverflow问题中看到了这一点,它引用了一个博客帖子。
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
guard self.dataSource?.tableView(tableView, canEditRowAt: indexPath) == true else {
return nil
}
let delete = UIContextualAction(style: .destructive, title: "Delete") { [weak self] action, view, success in
self?.remove(at: indexPath)
success(true)
}
let swipeActionConfig = UISwipeActionsConfiguration(actions: [delete])
swipeActionConfig.performsFirstActionWithFullSwipe = false
return swipeActionConfig
}https://stackoverflow.com/questions/71271441
复制相似问题