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

时间:2015-06-23 13:23:04

标签: 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]
}

任何帮助将不胜感激

5 个答案:

答案 0 :(得分:9)

我认为你在这里混合了两种不同的编辑方式。

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

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

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
从我的角度来看,甚至可能都没有打电话。

Apple的文档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) {

}

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

答案 1 :(得分:2)

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

答案 2 :(得分:1)

TLDR:

实施的必要功能: •commitEditingStyle •canEditRowAt •editActionsForRowAt

注意:对于editActionsForRowAt,你不能在这里返回一个空数组 - 它会搞砸所有单元格。如果存在特定类型的行,您不希望允许编辑操作,请在canEditRowAt中指定此类,以便为该类型的单元格返回false。

答案 3 :(得分:1)

很抱歉醒来这么老的线程,但是我在Swift 3上实现了它:

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    return .delete
}

答案 4 :(得分:0)

I just copied the ViewController where the row action buttons are shown and working well and replace the one where the row action buttons do not appear when sliding, and now the row actions buttons are shown and did the expected behavior.

But I do not understand the issue. does anyone can explain that?