如果条件在 swift 中为 false,则禁用在 tableView 中滑动单元格?

Kus*_*tha 2 uitableview swipe swift

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

    if condition == true {
    let pause = UITableViewRowAction(style: .Destructive, title: "pause") { action, index in
            print("pause button tapped")
            return [pause]
        }
    } else {

        return .None
    }
}
Run Code Online (Sandbox Code Playgroud)

此代码并未禁用在单元格中滑动。但是当滑动时会出现单元格删除选项。请任何人都可以帮助解决这个问题。

Nir*_*v D 5

您需要根据您的情况使用canEditRowAt它并返回值。Bool

override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {

    if condition == true {
        return true
    }
    return false
}
Run Code Online (Sandbox Code Playgroud)

无需检查editActionsForRowAtIndexPath方法中的条件。