2017-08-08 64 views
0

我试图改变UITableViewRowAction的标题,每次用户按下它就像完成/不完整,我写的代码把复选标记,但不改变标题,所以复选标记不能被删除:更改UITableViewRowAction swift的标题3

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] 
} 

有什么建议?

回答

0

这是我如何解决它:

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] 
}