我实现了 editActionsForRowAtIndexPath
和 commitEditingStyle
滑动正常,但 UITableViewCell
上没有出现任何编辑操作
我对 editActionsForRowAtIndexPath
和 commitEditingStyle
的实现如下:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
//I did some work here
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Delete" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
//I did some work here
tableView.reloadData()
})
return [deleteAction]
}
任何帮助将不胜感激
最佳答案
我认为您在这里混合了两种不同类型的编辑。
第一种编辑是旧的UITableViewCellEditingStyle.Delete
。新的方法是提供您的自定义附件 View 。
如果您实现自定义附件 View ,则不会显示默认删除按钮,因此不会调用。所以你的
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
从我的角度来看,甚至可能不会被调用。
Apple 的 editActionsForRowAtIndexPath
文档
包含以下句子:如果您不实现此方法,当用户滑动该行时, TableView 将显示标准附件按钮。
我假设如果您实现此方法,标准附件 View 将不显示。
编辑: 代码示例(更新至 Swift 3 11/17/16)
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
private func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: IndexPath) -> [AnyObject]? {
let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Delete" , handler: { (action:UITableViewRowAction, indexPath:IndexPath) -> Void in
})
return [deleteAction]
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
}
编辑 2: 正如 rajagp 指出的那样,如果您只针对 iOS9(或更高版本),则不需要空实现。
关于ios - 我实现了 editActionsForRowAtIndexPath 和 commitEditingStyle 但在滑动单元格时 tableViewCell 上没有出现任何编辑操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31004010/