Reputation: 11752
I have implemented swipe to delete on UITableView, it works correctly with default behaviour, i.e. content of cell is moving to the left while swiping like on the image. What I want to achive (customize) is to have this delete button appear over the cell content while swiping i.e. text in the cell isn't moved.
I am using this method
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
self.deletionDidStart(of: self.element(for: indexPath)!, at: indexPath)
// use helper function to delete element at given index path
let deleted = self.deleteElement(for: indexPath)
// refresh tableView
self.tableView.beginUpdates()
self.tableView.deleteRows(at: [indexPath], with: .automatic)
if let deletedSection = deleted.section {
self.tableView.deleteSections(IndexSet(integer: indexPath.section), with: .automatic)
self.deletedSection(deletedSection, at: indexPath)
}
self.tableView.endUpdates()
// call deletion event handler
self.deletedElement(deleted.element!, at: indexPath)
}
return [delete]
}
Upvotes: 0
Views: 508
Reputation: 6555
You should override setEditing
method of your UITableViewCell
,
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
let leadingSpace = 15
let editingOffset = 80
if editing == true {
self.labelLeadingConstraint.constant = CGFloat(leadingSpace + editingOffset)
} else {
self.labelLeadingConstraint.constant = CGFloat(leadingSpace);
}
UIView.animate(withDuration: 0.1) {
self.layoutIfNeeded()
}
}
You need to take IBOutlet
of your UILabel's
leading Constraint and do it like this way.
This is suggestion only, actual code may be changed depending on your Cell layout.
UPDATE
Use below functions as well.
func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
cell?.setEditing(true, animated: true)
}
func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) {
if let indexPath = indexPath {
let cell = tableView.cellForRow(at: indexPath)
cell?.setEditing(false, animated: true)
}
}
Hope this helps to you.
Upvotes: 1