I am using a the swipe to delete feature in the tableview to delete selected cells. You swipe it to the left to reveal a delete button. My code works well and deletes like it should.
I am also including a UIAlertController to give the user of the app a single last chance from making a mistake. What I would like to do is if the user selects "Cancel" is have the cell "un-swipe" and return to its original position. Is there a way to do this?
您可以关闭 tableView 上的编辑
[self.tableView setEditing:NO animated:YES];
I came across this question trying to do the same thing. I thought there had to be a better solution that reloading the table, so I did some further research and found the following.
tableView.setEditing(false, animated: true)
here's a little more code (Swift 3) to show it in use
// Custom cell edit actions
func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .destructive, title: "Delete") {(rowAction: UITableViewRowAction, indexPath: IndexPath) -> Void in
let refreshAlert = UIAlertController(title: "Delete", message: "Are you sure you want to delete this item.", preferredStyle: UIAlertControllerStyle.alert)
refreshAlert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action: UIAlertAction!) in
let context = self.fetchedResultsController.managedObjectContext
context.delete(self.fetchedResultsController.object(at: indexPath))
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}))
refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
// if the user presses cancel, slide the cell back
tableView.setEditing(false, animated: true)
}))
self.present(refreshAlert, animated: true, completion: nil)
}
let editAction = UITableViewRowAction(style: .normal, title: "Edit") { action, index in
print("edit button tapped")
}
editAction.backgroundColor = .orange
return [editAction, delete]
}
I have had a similar question and found your question searching for an answer. Based on the lack of answers here, I assumed I had to figure it out myself. Here is my issue and how I solved it. I hope it works for you, since I solved it extremely simple.
I have an app with a start screen that shows a table with several tracks. When you select a track you can either show the track details, show the track on a map (with MapView) or delete the track.
However, when you first use the app, the table is empty and shows a single table cell: "No saved tracks".
This means the table is empty and you can't show track details, a track map or delete the track. I wanted to make sure that if the table is empty, you get a different message and the left swipe is undone.
The code is an overige of funk -tableView:editActionsForRowAtIndexPath indexPath:
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let selectedRow = indexPath.row
guard self.savedTracks.count > 0 else {
//There are no saved tracks, the action cannot be executed.
let faultAction = UITableViewRowAction(style: .Normal, title: "No tracks", handler: { (action, indexPath) -> Void in
self.trackOverviewTable.reloadData() //Simply reload the table view after the action button is hit.
})
return [faultAction]
}
//Show track details
let detailsAction = UITableViewRowAction(style: .Default, title: "Details") { (action, indexPath) -> Void in
print("I now selected the track details view.\n")
self.newTrack = self.savedTracks[selectedRow]
self.performSegueWithIdentifier("showTrackDetails", sender: self)
}
//Show track map
let mapAction = UITableViewRowAction(style: .Normal, title: "Map") { (action, indexPath) -> Void in
print("I now selexted the track map view.\n")
self.newTrack = self.savedTracks[selectedRow]
self.performSegueWithIdentifier("showTrackMap", sender: self)
}
//Delete the selected track
let deleteAction = UITableViewRowAction(style: .Default, title: "Delete") { (action, indexPath) -> Void in
print("I now pressed the delete button.\n")
self.savedTracks.removeAtIndex(selectedRow)
if self.savedTracks.count > 0 {
self.trackOverviewTable.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else {
self.trackOverviewTable.reloadData()
}
}
detailsAction.backgroundColor = UIColor(colorLiteralRed: 21.0/255.0, green: 175.0/255.0, blue: 253.0/255.0, alpha: 0.8)
mapAction.backgroundColor = UIColor(colorLiteralRed: 37.0/255.0, green: 155.0/255.0, blue: 253.0/255.0, alpha: 0.8)
return [deleteAction, mapAction, detailsAction]
}
The important part here is the "guard else" statement, where the code is executed when there is no track saved. Basically, the swipi shows a different message "No tracks" and when this action button is clicked, the table will be reloaded and the swipe is undone. Since you use a UIAlertController, the same principle applies. The above methods works almost exactly the same as a UIAlertController and you can also reload your table after the user selects the "Cancel" button.
As I said, it is extremely simple and perhaps not meeting your expectations, but it does unswipe your left swipe after the user click "Cancel". I hope it works for you.
I have followed this approach and it works quite well
private func onDeleteNote(noteIdx: Int) {
let noteToDelete: Note = myNotes[noteIdx]
let alertViewController = UIAlertController(title: "¿Would you like to delete this note?", message: "The Note \(noteToDelete.title) will be delete", preferredStyle: .alert)
alertViewController.addAction(UIAlertAction(title: "Delete", style: .destructive) { (UIAlertAction) in
print("Delete note")
})
alertViewController.addAction(UIAlertAction(title: "Cancel", style: .cancel) { (UIAlertAction) in
print("Delete note was cancel")
self.myNotesTable.reloadRows(at: [IndexPath(row: noteIdx, section: 0)], with: .right)
})
present(alertViewController, animated: true, completion: nil)
}
When the user cancel the operation I try to reload this table cell for discarding the swipe state.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.