0

I am trying the change the title of UITableViewRowAction each time the user press on it like complete/incomplete, the code I wrote puts the checkmark but does not change the title, so the checkmark can not be removed:

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let completeAction = UITableViewRowAction(style: .normal, title: "Complete", handler: { (action, indexPath) in
        if action.title == "Complete"{
            action.title = "Incomplete"
            cell?.accessoryType = UITableViewCellAccessoryType.checkmark
        }
        else{
            cell?.accessoryType = UITableViewCellAccessoryType.none
            action.title = "Complete"
        }
        tableView.setEditing(false, animated: true)
    })
    completeAction.backgroundColor = .blue
    return [completeAction]
}

Any advice?

Mireille
  • 637
  • 2
  • 12
  • 27

1 Answers1

0

This is how I solved it:

var complete:Bool = false
{
    didSet{
        if complete {
            comp = "Incomplete"
        }
        else{
            comp = "Complete"
        }
    }
}
var comp:String = "Complete"

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let cell = tableView.cellForRow(at: indexPath)
    let completeAction = UITableViewRowAction(style: .normal, title: "\(comp)", handler: { (action, indexPath) in
        if action.title == "Complete"{
            self.complete = true
            cell?.accessoryType = UITableViewCellAccessoryType.checkmark
        }
        else{
            self.complete = false
            cell?.accessoryType = UITableViewCellAccessoryType.none
        }
        tableView.setEditing(false, animated: true)
    })
    completeAction.backgroundColor = .blue
    return [completeAction]
}
Mireille
  • 637
  • 2
  • 12
  • 27