Zac
Zac

Reputation: 423

UITableViewRowAction not working

I have an UITableView that I manage in a controller with UITableViewDelegate and UITableViewDataSource. In this table I have a custom cell, the problem is that the function editActionsForRowAtIndexPath gets called only sometimes (maybe when I swype in a particular way, I don't know), my code is the following:

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
    let doneAction: UITableViewRowAction
    //let highlightAction: UITableViewRowAction
    if(self.compiti[indexPath.row].completato){
        doneAction = UITableViewRowAction(style: .Normal, title: "Da Fare") { (UITableViewRowAction, indexPath: NSIndexPath!) -> Void in
            let compito = self.compiti[indexPath.row]
            self.db.contrassegnaCompito(compito)
            UITableViewRowAction
       }
        doneAction.backgroundColor = UIColor.redColor()
    }else{
        doneAction = UITableViewRowAction(style: .Normal, title: "Fatto") { (UITableViewRowAction, indexPath: NSIndexPath!) -> Void in
            let compito = self.compiti[indexPath.row]
            self.db.contrassegnaCompito(compito)
        }
        doneAction.backgroundColor = UIColor(red: 67/255, green: 160/255, blue: 71/255, alpha: 0.7)
    }
    return [doneAction]
}

Upvotes: 2

Views: 1084

Answers (2)

zepar
zepar

Reputation: 165

you need to add this method implementation as well or you won't be able to swipe to display the actions

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    // it can be empty
}

Upvotes: 3

Mat Grlt
Mat Grlt

Reputation: 131

It works for me with this code, try starting from this and you will probably find when the problem occurs

 func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
    var doneAction :UITableViewRowAction!

    doneAction = UITableViewRowAction(style: .Default, title: "Da Fare") { (UITableViewRowAction, indexPath: NSIndexPath!) -> Void in
            UITableViewRowAction
    }
    return [doneAction]
}

Upvotes: 0

Related Questions