Ios 要删除的TableView滑动:如何以编程方式取消滑动
我正在使用tableview中的“滑动删除”功能删除选定的单元格。将其向左滑动可显示删除按钮。我的代码运行良好,可以正常删除Ios 要删除的TableView滑动:如何以编程方式取消滑动,ios,swift,uitableview,Ios,Swift,Uitableview,我正在使用tableview中的“滑动删除”功能删除选定的单元格。将其向左滑动可显示删除按钮。我的代码运行良好,可以正常删除 我还包括一个UIAlertController,让应用程序的用户有最后一次机会避免犯错误。我想做的是,如果用户选择“取消”是让单元格“取消滑动”并返回其原始位置。有没有办法做到这一点?您可以关闭tableView上的编辑功能 [self.tableView setEditing:NO animated:YES]; 我有一个类似的问题,发现你的问题正在寻找答案。基于这里缺
我还包括一个UIAlertController,让应用程序的用户有最后一次机会避免犯错误。我想做的是,如果用户选择“取消”是让单元格“取消滑动”并返回其原始位置。有没有办法做到这一点?您可以关闭tableView上的编辑功能
[self.tableView setEditing:NO animated:YES];
我有一个类似的问题,发现你的问题正在寻找答案。基于这里缺乏答案,我认为我必须自己解决这个问题。这是我的问题以及我是如何解决的。我希望它对你有用,因为我解决它非常简单 我有一个带有开始屏幕的应用程序,它显示了一个包含多个曲目的表格。选择轨迹时,可以显示轨迹详细信息、在地图上显示轨迹(使用MapView)或删除轨迹 但是,当您第一次使用该应用程序时,该表是空的,并显示一个表格单元格:“没有保存的曲目” 这意味着该表为空,无法显示轨迹详细信息、轨迹地图或删除轨迹。我想确保如果表是空的,您会收到另一条消息,并且左扫被取消 代码是对funk-tableView:editActionsForRowAtIndexPath:
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]
}
这里的重要部分是“guard else”语句,在该语句中,代码在没有保存任何轨迹时执行。基本上,swipi会显示一条不同的消息“No tracks”,单击此操作按钮时,将重新加载表格并取消滑动。由于使用UIAlertController,同样的原则也适用。上述方法的工作原理与UIAlertController几乎完全相同,您也可以在用户选择“取消”按钮后重新加载表
正如我所说的,它非常简单,可能没有达到您的期望,但它不会在用户单击“取消”后取消您的左扫。我希望它对你有用。我在尝试做同样的事情时遇到了这个问题。我认为必须有一个更好的解决方案来重新加载表,所以我做了一些进一步的研究,发现如下
tableView.setEditing(false, animated: true)
这里有更多的代码(Swift 3)来展示它的使用
// 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]
}
我采用了这种方法,效果很好
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)
}
当用户取消操作时,我尝试重新加载此表单元格以放弃滑动状态。在deleteRowsAtIndexPath之前复制所有单元格数据,如果他们点击取消,只需将其交换回并重新加载tableView。从iOS11开始,这不再需要(实际上也不起作用)。要以编程方式取消滑动视图,请使用true或false值调用“completionHandler”(设置滑动操作时UIContextualAction.Handler的第三个参数)。