Changing the UITableViewCell swipe action title

╄→尐↘猪︶ㄣ 提交于 2020-01-17 03:34:56

问题


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

}


回答1:


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]

}


来源:https://stackoverflow.com/questions/35208657/changing-the-uitableviewcell-swipe-action-title

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!