2017-08-08 63 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] 
}