[英]UITableView disable swipe to delete for particular cells swift
我正在尝试禁用滑动以删除 tableview 中某些特定单元格的操作。 但我无法迅速找到一个好的解决方案。 当覆盖“tableView.isEditing = true”方法时,我不希望单元格上的左侧减号。这是我的代码。 下面的代码没有禁止滑动到 statusId 为“12”的单元格。 希望你明白我的问题。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath) as! TableViewDateCell
cell.titleStatus.text = mainList[indexPath.row].statusName
//tableView.isEditing = true
//disable swipe to delete for cell with statusId == "12"
if mainList[indexPath.row].statusId == "12" {
//tableView.isEditing = false
cell.selectionStyle = UITableViewCellSelectionStyle.gray
cell.isUserInteractionEnabled = false
}
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
}
您可以自定义UITableViewDelegate
的函数editingStyleForRowAt
,尤其是在不需要滑动时返回UITableViewCellEditingStyle.none
,例如:
public func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle
{
if mainList[indexPath.row].statusId == "12" {
return UITableViewCellEditingStyle.none
} else {
return UITableViewCellEditingStyle.delete
}
}
使用这个表视图委托方法
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
}
感谢您发布这个问题和上面的答案。
在答案的扩展中,我有一个永远不应删除的默认行,因此我在该特定单元格上设置了一个标签,然后首先使用了此方法。
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
if tableView.cellForRow(at: indexPath)?.tag == 100 {
return false
}
return true
}
但是- 这导致调试器中出现以下警告 -
尝试在表视图上调用 -cellForRowAtIndexPath: 在更新其可见单元格的过程中,这是不允许的。
因此,正如其他评论员所说,请使用以下方法:
斯威夫特 5.0
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
if tableView.cellForRow(at: indexPath)?.tag == 100 {
return UITableViewCell.EditingStyle.none
} else {
return UITableViewCell.EditingStyle.delete
}
}
我在编辑 myTableview 时试图禁用/启用删除功能。 这是我的解决方案:
public func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle
{
if !myTableView.isEditing {
return UITableViewCell.EditingStyle.none
} else {
return UITableViewCell.EditingStyle.delete
}
}
viewDidLoad 中的 Swift 4.2 只需将您的表值设置为...
myTableView.isEditing = false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.