2017-04-26 104 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

多数民众赞成在.. .. :) –