Home > Mobile >  UITableViewCell - distinguish full edit mode and delete row mode
UITableViewCell - distinguish full edit mode and delete row mode

Time:01-10

I have UITableViewController and after user pres desired button, I have "full edit" mode. To enable this, I call this:

@objc func btnEditPressed(sender: UIButton) {
   sender.isSelected = !self.isEditing
   self.setEditing(!self.isEditing, animated: true)

   self.tableView.beginUpdates()
   self.tableView.endUpdates()
}

for each UITableViewCell, I have override func setEditing(_ editing: Bool, animated: Bool) method. In this, I hide some parts of cell in "full edit" mode.

Now, when the user swipe single table row, I show the delete button. However, in this "delete row mode" I dont want to hide any information from the cell. The problem is, that the same override func setEditing(_ editing: Bool, animated: Bool) for the cell is called as in the first case, leading to hiding cell content.

Is there an easy way, how to solve this, or I have to keep weak reference to UITableViewController and check the mode from the cell myself?

CodePudding user response:

You have two options to handle this.

  1. Check showingDeleteConfirmation in the cell's setEditing method.
  2. Override willTransition(to:) and didTransition(to:) in your cell class.

If your table view is not in editing mode and the user performs a swipe-to-delete gesture, then your cell will experience the following sequence:

  • willTransition(to:) will be called with the mask including the value UITableViewCell.StateMask.showingDeleteConfirmation
  • setEditing will be called with editing set to true. The cell's showingDeleteConfirmation will be equal to true
  • didTransition(to:) will be called with the mask including the value UITableViewCell.StateMask.showingDeleteConfirmation

So the simplest solution is to update your cell's setEditing:

override func setEditing(_ editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)

    if showingDeleteConfirmation {
        // User did a swipe-to-delete
    } else {
        // The table view is in full edit mode
    }
}
  • Related