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.
- Check
showingDeleteConfirmation
in the cell'ssetEditing
method. - Override
willTransition(to:)
anddidTransition(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 valueUITableViewCell.StateMask.showingDeleteConfirmation
setEditing
will be called withediting
set totrue
. The cell'sshowingDeleteConfirmation
will be equal totrue
didTransition(to:)
will be called with the mask including the valueUITableViewCell.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
}
}