ios - 我实现了 editActionsForRowAtIndexPath 和 commitEditingStyle 但在滑动单元格时 tableViewCell 上没有出现任何编辑操作

标签 ios swift uitableview tableviewcell uitableviewrowaction

我实现了 editActionsForRowAtIndexPathcommitEditingStyle 滑动正常,但 UITableViewCell 上没有出现任何编辑操作

我对 editActionsForRowAtIndexPathcommitEditingStyle 的实现如下:

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/

相关文章:

iphone - 自定义 UITableViewCell 未显示时始终为 null

ios - UIImage 从 json 请求返回 nil 到 tableView 单元格图像的 mysql 数据库?

ios - 带有自调整单元格的 UICollectionView 无法正确计算 contentSize

ios - 将库导入 Xcode 7 项目

ios - 改变异步 block 内的输入函数参数

ios - uitableviewcell 中的自动布局问题

ios - 将 dbmanager 数据填充到 UITableView

ios - 作为 C 指针类型的 Objective-C 类

iphone - 如何在 ios 中点击 url 时将字节码转换为 pdf

ios - 为什么我在 UI 自动化工具中看到 "An error occurred while trying to run the script."?