Swift editActionsForRowAtIndexPath

时间:2014-10-05 19:27:55

标签: ios swift tableview

我正在尝试在tableview中添加一些编辑操作。但是,我无法弄清楚要为UITableViewRowAction添加什么!它在代码中表示为 IDK

func Done(UITableViewRowAction!,
    NSIndexPath!) -> Void {

}

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {

    let complete = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Done", handler: Done(_IDK_, indexPath))

    let arrayofactions: Array = [complete]

    return arrayofactions
}

4 个答案:

答案 0 :(得分:3)

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
    let delete = UITableViewRowAction(style: .Normal, title: "Delete") { action, index in
        println("delete")
    }
    let done = UITableViewRowAction(style: .Default, title: "Done") { action, index in
        println("done")
    }
    return [delete, done]
}

答案 1 :(得分:0)

在这种情况下,闭包对我有效。

func Done(UITableViewRowAction!,
NSIndexPath!) -> Void {

}

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath:      NSIndexPath) -> [AnyObject]? {

let complete = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Done", handler: { (action, indexPath.row) -> Void in 
 'handle your action here'

})

let arrayofactions: Array = [complete]

return arrayofactions
}

我发现了一篇关于此的博文,对我有用。

答案 2 :(得分:0)

Swift 2.2

我必须以这种方式更新

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool 
{
   return true
}

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? 
{
    let delete = UITableViewRowAction(style: .Normal, title: "Delete") 
    { action, index in
        print("delete")
    }
    let done = UITableViewRowAction(style: .Default, title: "Done") 
    { action, index in
        print("done")
    }
    return [delete, done]
}

答案 3 :(得分:-1)

添加此方法

- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath
{
// All tasks are handled by blocks defined in editActionsForRowAtIndexPath, however iOS8 requires this method to enable editing
}