仅在某些单元格上允许滑动

Swiping Allowed Only On Certain Cells

我有一个 table 视图,它会根据按下的按钮更改其单元格。在第一个 table 视图中,我希望只有在按下按钮时才能滑动单元格;如果不是,则应禁用滑动功能。按下按钮时,table 视图可滑动并显示我想要的操作。但是当没有按下按钮时,单元格仍然可以通过 "delete" 动作滑动。如果单元格标签包含当前用户名,我也不希望一行可以滑动。

我在底部添加了 table 视图的代码。我该如何解决?

第一个 table 的代码:

 override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
                let cell = tableView.cellForRow(at: indexPath) as! DetailsCell
                let detailsAction = UITableViewRowAction(style: UITableViewRowActionStyle.default, title: "Details") { (action , indexPath) -> Void in
                        if isCharacterButtonPressed == true { //if button is pressed
                            if cell.label != currentUser.name{
                                let viewController = self.storyboard?.instantiateViewController(withIdentifier: "SeeDetails") as! DetailsVC
                                viewController.person = cell.person            
        self.navigationController?.pushViewController(viewController, animated: true)
                            } else {
                              return .none
                             }
                            return [detailsAction] //makes cell swipe-able
                        } else { //if button is not pressed
                            return .none //do not make the cell swipe-able
                        }
               }

编辑:

我已经用 canEditRow 函数修复了它,但是当我滑动它们时,这些红色圆圈出现在可以滑动的单元格的一侧。我该如何解决?我附上了它的截图:

如果在tableView(_: UITableView, editActionsForRowAt: IndexPath)中检查是否显示编辑操作,用户体验会很糟糕,因为当return一个空的操作数组不显示任何编辑操作时,单元格出现就好像它被刷了一会儿然后停止响应刷卡一样。相反,您应该使用 tableview(_:UITableView,canEditRowAt:IndexPath) 检查是否显示单元格的编辑操作,然后使用其他函数在单元格可滑动时实际显示操作。

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
   let cell = tableView.cellForRow(at: indexPath) as! DetailsCell
   if isCharacterButtonPressed == true && cell.label != currentUser.name{
      return true
   } else {
      return false
   }
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
   let cell = tableView.cellForRow(at: indexPath) as! DetailsCell
   let detailsAction = UITableViewRowAction(style: UITableViewRowActionStyle.default, title: "Details") { (action , indexPath) -> Void in
     let viewController = self.storyboard?.instantiateViewController(withIdentifier: "SeeDetails") as! DetailsVC
     viewController.person = cell.person            
     self.navigationController?.pushViewController(viewController, animated: true)
   }        
  return [detailsAction]        
}

使用 tableView(_: UITableView, editActionsForRowAt: IndexPath)MGSwipTableCell,创建一个 bool(即 allowSwipe),仅当 bool 为真时才允许 editing/swiping