UITableView禁用滑动以删除特定单元格swift

Joh*_*ohn 7 uitableview ios 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) {

}
Run Code Online (Sandbox Code Playgroud)

And*_*ini 16

您可以自定义该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
    }
}
Run Code Online (Sandbox Code Playgroud)