I am new to Swift (and to coding), so please bear with me. On my table, I have set up the cells so that when I swipe right, I see two options: "done" and "delete." When I click on "delete," the cell row is removed. When I click on "done," I want the cell row to change color. I don't know how I got to this point, but when I click on "done," the whole table changes color except for the cell row. I tried to use my reusable identifier for the cell prototype, which I set as "cell," but that didn't work out because I'm not sure on how to declare the "cell" without it interfering with my CellForRowAt function.
Here is my code:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
// delete item at indexPath
self.habits.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
print(self.habits)
}
let done = UITableViewRowAction(style: .default, title: "Done") { (action, indexPath) in
// save item at indexPath
tableView.backgroundColor = UIColor.lightGray
print("Done")
}
done.backgroundColor = UIColor.green
return [delete, done]
}
I don't think I can declare "cell" because of this previous block of code:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Dequeue Resuable Cell
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") ?? UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
let Habit = habits[indexPath.row]
cell.textLabel?.text = Habit.mainGoal
cell.detailTextLabel?.text = Habit.microGoal
return cell
Again, I am new to coding. I already tried looking up this question and not quite sure if I am not wording it correctly, but I can't find the answer. My backup idea is to strikethrough the labels in the cell, but from what I gathered, that is more complicated.
You can acheave this by doing
let done = UITableViewRowAction(style: .default, title: "Done") { (action, indexPath) in
// save item at indexPath
let cell = tableView.cellForRow(at: indexPath)
cell?.backgroundColor = UIColor.green
print("Done")
}