// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
tableView.moveRow(at: fromIndexPath, to: to)
}
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
true
}
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { (action, view, completionHandler) in
// // 删除数据
// self?.data.remove(at: indexPath.row)
//
// 删除行
tableView.deleteRows(at: [indexPath], with: .fade)
completionHandler(true)
}
let swipeConfig = UISwipeActionsConfiguration(actions: [deleteAction])
swipeConfig.performsFirstActionWithFullSwipe = true
return swipeConfig
}
3. 但是这两者不能共存,即编辑模式下,左滑失效了。 当你想要排序和左滑删除功能,又不想要编辑模式下的左边红色减号按钮,就只能想办法自己实现排序功能了。 好在iOS 11 之后有了新的Drag And Drop 功能:
tableView.dragInteractionEnabled = true
tableView.dragDelegate = self
tableView.dropDelegate = self
具体实现查看:https://developer.apple.com/documentation/uikit/drag_and_drop/adopting_drag_and_drop_in_a_table_view
或者参考:
- SwipeCellKit: 专门用于左右滑动的菜单,但是在UITableView 为 insetGroup 时有一个崩溃 bug
- LPRTableView:长按排序功能