如果我有2-3个TableView,我如何才能仅对特定的TableView禁用'Delete‘行?当我为if语句设置断点以检查使用了哪个tableView时,它不工作
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if tableView == self.firstTableView {
if editingStyle == .delete {
array.remove(at: indexPath.row)
firstTableView.deleteRows(at: [indexPath], with: .fade)
firstTableView.reloadData()
}
}
}我尝试在secondTableView的viewDidLoad中将编辑模式设置为false,但也不起作用。
secondTableView.setEditing(false, animated: false)我知道默认情况下它被设置为false,所以我想如果commit editingStyle对所有的tableViews都启用它,那么我可以禁用它一秒钟。
发布于 2017-07-18 06:04:19
只需给每个TableView一个标记,并在if或switch语句中检查它。
if tableView.tag == 0 {
if editingStyle == .delete {
array.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
tableView.reloadData()
}
}发布于 2017-07-18 10:28:19
正确的答案是检查editingStyleForRowAt indexPath中的标签
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
if tableView.tag == 1 {
return UITableViewCellEditingStyle.delete
} else {
return UITableViewCellEditingStyle.none
}
}发布于 2017-07-18 12:27:14
您可以使用:
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item or table to be editable.
if tableView == tableVw {
return false
} else {
return true
}
}这里tableVw是一个你不想编辑的tableview对象,或者你也可以用标签代替object compare。然后使用:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
//Write your delete cell logic here
array.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
tableView.reloadData()
}
}https://stackoverflow.com/questions/45152117
复制相似问题