我实现了editActionsForRowAtIndexPath和commitEditingStyle,但在刷单元格时,tableViewCell上没有显示编辑操作

Abd*_*man 8 uitableview tableviewcell ios swift 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]
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激

小智 9

我想你在这里混合了两种不同的编辑方式.

第一种编辑是旧的UITableViewCellEditingStyle.Delete.新方法是提供您的自定义配件视图.

如果您实现自定义附件视图,则不会显示默认删除按钮,因此不会调用.所以你的

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
Run Code Online (Sandbox Code Playgroud)

从我的角度来看,甚至可能都没有被召唤.

Apple的文档For editActionsForRowAtIndexPath 包含以下内容:If you do not implement this method, the table view displays the standard accessory buttons when the user swipes the row.我假设如果您实现此方法,则不会显示标准附件视图.

编辑:代码示例(更新到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) {

}
Run Code Online (Sandbox Code Playgroud)

编辑2:正如rajagp指出的那样,如果你只是针对iOS9(或更高版本),则不需要空实现.


Mat*_*ees 3

您需要从 UITableview 委托方法实现canEditRowAtIndexPath并返回 true。