如何禁用 UITableViewRowAction(不单击编辑按钮)?
How to disable UITableViewRowAction (without clicking an edit button)?
通常调用此函数时,允许向左滑动删除table视图单元格:
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
dataSource.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.fade)
}
}
但是我创建了一个这样的编辑按钮:
self.navigationItem.leftBarButtonItem = self.editButtonItem
并且我想仅在单击编辑按钮时启用删除单元格,而不仅仅是通过滑动。这可能吗?
使用默认值 false
创建一个 Boolean
实例 属性 并在使用该布尔值之后根据 BarButttonItem
的操作将其设置为 true
属性 在 canEditRowAt
方法中对 allow/disallow 行进行编辑。
var allowEdit = false
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return allowEdit
}
注意:删除完成后不要忘记再次设置false
。
我是这样解决的:
var editMode = false
...
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
if editMode == true {
return true
} else {
return false
}
}
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
if(editing) {
tableView.reloadData()
editMode = true
}
else if(!editing) {
tableView.reloadData()
editMode = false
}
}
通常调用此函数时,允许向左滑动删除table视图单元格:
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
dataSource.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.fade)
}
}
但是我创建了一个这样的编辑按钮:
self.navigationItem.leftBarButtonItem = self.editButtonItem
并且我想仅在单击编辑按钮时启用删除单元格,而不仅仅是通过滑动。这可能吗?
使用默认值 false
创建一个 Boolean
实例 属性 并在使用该布尔值之后根据 BarButttonItem
的操作将其设置为 true
属性 在 canEditRowAt
方法中对 allow/disallow 行进行编辑。
var allowEdit = false
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return allowEdit
}
注意:删除完成后不要忘记再次设置false
。
我是这样解决的:
var editMode = false
...
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
if editMode == true {
return true
} else {
return false
}
}
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
if(editing) {
tableView.reloadData()
editMode = true
}
else if(!editing) {
tableView.reloadData()
editMode = false
}
}