IOS 8 UItableView滑动删除

时间:2014-12-16 16:55:53

标签: uitableview ios8 delete-row swipe-gesture

我有UIviewController,其中包含一个UITableView,它是一个非常简单的代码,我只想刷一个单元格并显示更多并删除和处理这两个事件,这是我非常简单的类

class ViewController: UIViewController  , UITableViewDataSource , UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

let deleteClosure = { (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in
    println("Delete closure called")
}

let moreClosure = { (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in
    println("More closure called")
}



override func viewDidLoad()
{
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let identifier = "Cell"
    let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = "\(indexPath.row)"

    return cell
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 100
}


func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]?
{
    var moreAction = UITableViewRowAction(style: .Default , title: "More") { (action : UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in

        println("more")
    }
    moreAction.backgroundColor = UIColor.lightGrayColor()

    var deleteAction = UITableViewRowAction(style: .Default, title: "Delete") { (action: UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in

        println("delete")
    }

    return [moreAction , deleteAction]
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
    if (editingStyle == .Delete)
    {
        println("delete1")

    }
    if(editingStyle == .Insert)
    {
        println("insert")

    }

}

调用editActionsForRowAtIndexPath函数,但没有任何RowActions出现! 有什么问题 ? ,

1 个答案:

答案 0 :(得分:1)

您需要在自定义按钮的处理程序中执行某些操作。

例如,使用自定义删除按钮(并且只是为了更改其标签和背景颜色),您可以执行以下操作:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
    let button = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: NSLocalizedString("TitleOfDeleteButton", comment: "Delete"), handler: { (rowAction, indexPath) -> Void in
        self.tableView(self.tableView!, commitEditingStyle: UITableViewCellEditingStyle.Delete, forRowAtIndexPath: indexPath)
    })
    button.backgroundColor = UIColor.lightGrayColor()
    return [button]
}

另外,不要忘记UITableViewDelegate协议的这种方法仅适用于iOS8