SWIFT:滑动表格单元格以更改它';s颜色
我是Swift(和编码)新手,所以请容忍我。在我的表格上,我设置了单元格,这样当我向右滑动时,我会看到两个选项:“完成”和“删除”。当我单击“删除”时,单元格行被删除。单击“完成”时,我希望单元格行更改颜色。我不知道我是如何做到这一点的,但当我点击“完成”时,除了单元格行之外,整个表格都会改变颜色。我试着使用我的可重用标识符作为cell原型,我将其设置为“cell”,但没有成功,因为我不知道如何声明“cell”,而不干扰我的CellForRowAt函数 这是我的密码:SWIFT:滑动表格单元格以更改它';s颜色,swift,uitableview,row,cell,Swift,Uitableview,Row,Cell,我是Swift(和编码)新手,所以请容忍我。在我的表格上,我设置了单元格,这样当我向右滑动时,我会看到两个选项:“完成”和“删除”。当我单击“删除”时,单元格行被删除。单击“完成”时,我希望单元格行更改颜色。我不知道我是如何做到这一点的,但当我点击“完成”时,除了单元格行之外,整个表格都会改变颜色。我试着使用我的可重用标识符作为cell原型,我将其设置为“cell”,但没有成功,因为我不知道如何声明“cell”,而不干扰我的CellForRowAt函数 这是我的密码: func ta
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]
}
我不认为我可以声明“cell”,因为前面的代码块:
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
同样,我对编码是新手。我已经试着查找这个问题,不太确定我的措辞是否正确,但我找不到答案。我的备份想法是删除单元格中的标签,但从我收集的信息来看,这更为复杂。在这里,您可以找到实现要求的可能方法
您可以通过执行
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")
}
这不是问题,太谢谢你了!!如果我想让它每天重置,那会是另一回事吗?或者我可以把它包含在这个函数的某个地方吗?