2017-04-26 103 views
0

我在ViewController中有一個tableView,我只想在用戶單擊單元格中的Edit按鈕時更改單元格背景顏色。單擊編輯按鈕時更改單元格顏色

我做了什麼至今

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "taskcell", for: indexPath as IndexPath) as! taskListCell 
    cell.backgroundColor = colorMediumGreen 
    // Configure the cell… 
    let tableData = self.tableData[indexPath.row] 
    cell.duration.text = tableData[""] as? String 
    cell.taskDetail.text = tableData[""] as? String 

    return cell 

} 
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 

    let editAction = UITableViewRowAction(style: .normal, title: "Erledigt") { (rowAction, indexPath) in 

    } 

    editAction.backgroundColor = .red 

    return [editAction] 
} 
+0

剛剛獲得與IndexPath'的'幫助小區和更新''editAction該小區。 –

+0

@DharmeshKheni:你能解釋一下嗎? – SwiftUser

回答

1

我已經解決了這個問題:-)

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 


    let editAction = UITableViewRowAction(style: .normal, title: "Edit") { (rowAction, indexPath) in 

     let cell = self.tableView(tableView, cellForRowAt: indexPath) 
     cell.contentView.backgroundColor = colorLightGray 

     self.tableView.reloadData() 
     self.tableView.endUpdates() 
    } 

    editAction.backgroundColor = .red 

    return [editAction] 
} 
+0

多數民衆贊成在.. .. :) –