ap123
ap123

Reputation: 986

Swiping Allowed Only On Certain Cells

I have a tableview that will change its cells based on what button is pressed. In the first table view I want to be able to swipe the cell only if the button is pressed; if it's not then the swipe function should be disabled. When the button is pressed, tableview is swipe-able and shows the action I want. But when the button is not pressed, the cell is still swipe-able with a "delete" action. I also do not want a row to be swipe-able if the cells label contains the current user's name.

I have added my code for the tableview at the bottom. How do I fix this?

Code for the 1st 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
                        }
               }

EDIT:

I have fixed it with the canEditRow function, but these red circles show up on the side of the cells that can be swiped when I swipe them. How do I fix this? I have attached a screenshot of it: enter image description here

Upvotes: 0

Views: 1517

Answers (2)

Josh O'Connor
Josh O'Connor

Reputation: 4962

Use either tableView(_: UITableView, editActionsForRowAt: IndexPath) or MGSwipTableCell, create a bool (i.e allowSwipe) , only allow editing/swiping if the bool is true

Upvotes: 0

Santhosh R
Santhosh R

Reputation: 1568

If you check whether to display edit actions or not in tableView(_: UITableView, editActionsForRowAt: IndexPath), the user experience will be sloppy because when return an empty array of actions to not show any edit actions, the cell appears as if it got swiped for a moment and then stops responding to the swipe. Instead you should check whether to display the edit actions for a cell or not, using tableview(_:UITableView,canEditRowAt:IndexPath) and then use the other function to actually display the actions when the cell is swipeable.

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]        
}

Upvotes: 7

Related Questions