简体   繁体   English

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

[英]UITableView disable swipe to delete for particular cells swift

I am trying to disable swipe to delete action for some particular cells in tableview.我正在尝试禁用滑动以删除 tableview 中某些特定单元格的操作。 but i could not be find a good solution in swift.但我无法迅速找到一个好的解决方案。 I don't want that left side minus sign on cell when overriding "tableView.isEditing = true" method.Here is my code.当覆盖“tableView.isEditing = true”方法时,我不希望单元格上的左侧减号。这是我的代码。 below code is not disabling swipe to the cell with statusId "12".下面的代码没有禁止滑动到 statusId 为“12”的单元格。 Hope you understand my problem.希望你明白我的问题。

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) {

}

You might customize the UITableViewDelegate 's function editingStyleForRowAt , especially returning UITableViewCellEditingStyle.none when you don't need the swipe, something like:您可以自定义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
    }
}

Use this table view delegate method使用这个表视图委托方法

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {

}

Documentation tableView:canEditRowAtIndexPath: 文档 tableView:canEditRowAtIndexPath:

Thanks for posting this question and the answers above.感谢您发布这个问题和上面的答案。

On an expansion of the answers, I had a default row that should never be deleted, so I set a tag on that particular cell and then at first used this method.在答案的扩展中,我有一个永远不应删除的默认行,因此我在该特定单元格上设置了一个标签,然后首先使用了此方法。

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    if tableView.cellForRow(at: indexPath)?.tag == 100 {
        return false
    }
    return true
}

HOWEVER - This caused the following warning in Debugger -但是- 这导致调试器中出现以下警告 -

Attempted to call -cellForRowAtIndexPath: on the table view while it was in the process of updating its visible cells, which is not allowed.尝试在表视图上调用 -cellForRowAtIndexPath: 在更新其可见单元格的过程中,这是不允许的。

So, as other commentators have said, use this method below:因此,正如其他评论员所说,请使用以下方法:

Swift 5.0斯威夫特 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
    }
}

i was trying to disable/enable the delete feature when i edit the myTableview.我在编辑 myTableview 时试图禁用/启用删除功能。 this is my solution:这是我的解决方案:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM