ios - 如何在tableview编辑模式下滑动删除?(迅速)
问题描述
我想让表格视图可以重新排序。
它有汉堡按钮来重新排序单元格,我可以像 iOS 音乐应用程序一样滑动单元格来删除单元格。
所以我这样做了,我不能刷单元格。我认为 tableView.setEditing 使单元格无法滑动。
我怎样才能做到?
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let action = UIContextualAction(style: .normal, title: nil) { (action, view, completion) in
self.shoppingList.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
completion(true)
}
action.backgroundColor = .white
action.image = UIImage(named: "delete")
let configuration = UISwipeActionsConfiguration(actions: [action])
configuration.performsFirstActionWithFullSwipe = false
return configuration
}
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .none
}
func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
return false
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let movedCell = self.shoppingList[sourceIndexPath.row]
shoppingList.remove(at: sourceIndexPath.row)
shoppingList.insert(movedCell, at: destinationIndexPath.row)
}
override func viewDidLoad() {
super.viewDidLoad()
setup()
bindConstraints()
}
func setup() {
let footerView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 39))
footerView.addSubview(writeButton)
tableView.delegate = self
tableView.dataSource = self
tableView.tableFooterView = footerView
//tableView.sectionFooterHeight = 100
self.view.addSubview(tableView)
tableView.setEditing(true, animated: true)
}
解决方案
尝试
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return .delete
}
推荐阅读
- r - 合并具有部分相同值的行
- python - Python混淆中的多重继承
- java - 为什么Java JPackage 在两个地方安装Windows dll 文件?
- flutter - 带有原生分配的 Flutter Isolate.kill()
- firebase - 如何在使用 @firebase/testing 编写集成测试时调用 Firebase HTTPS 可调用函数?
- javascript - TodoList 应用程序,如何在一页中限制 10 个输入数据?
- python - 并发使用多个 Python 版本
- robotframework - 无法在 Robot Framework 中使用 Get Element 获取元素
- flutter - 我们可以在 Flutter 中的 Razorpay 中实现自定义结帐屏幕吗
- docker - Alpine:不可满足的约束