Search code examples
iosswiftuitableviewcocoa-touchuitableviewrowaction

Changing the UITableViewCell swipe action title


I have a row action button titled "Activate". When I press it, I want the title to change to "Deactivate".

I was unable to change the title in the handler function. Also, "Activate" and "Deactivate" should have different functions.

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

    let account = accountTypes[indexPath.section].accounts[indexPath.row]

    let delete = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete", handler: { (action, indexPath) -> Void in
        // action delete
    })

    let activation = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Activate", handler: { (action, indexPath) -> Void in
        // action activate
    })
    activation.backgroundColor = UIColor.lightGrayColor()

    let arrayofactions: Array = [delete, activation]
    return arrayofactions

}


Solution

  • You have to manage your cell's state manually for each indexpath when you want to show activate and de-activate. Save data(condition) for individual indexpath and put below condition in your editActionsForRowAtIndexPath method.

    let arrayofactions
    if(condition true) {
      let activation = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Activate", handler: { (action, indexPath) -> Void in
        // action activate
      })
       activation.backgroundColor = UIColor.lightGrayColor()
       arrayofactions: Array = [delete, activation]
    }
    else {
       let deactivation = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "De-Activate", handler: { (action, indexPath) -> Void in
        // action deactivate
      })
       deactivation.backgroundColor = UIColor.lightGrayColor()
       deactivation: Array = [delete, deactivation]
    
    }