gpt4 book ai didi

SWIFT:滑动表格单元格以更改其颜色

转载 作者:行者123 更新时间:2023-11-30 11:18:16 25 4
gpt4 key购买 nike

我是 Swift(和编码)新手,所以请耐心等待。在我的 table 上,我设置了单元格,以便当我向右滑动时,我会看到两个选项:“完成”和“删除”。当我单击“删除”时,单元格行将被删除。当我单击“完成”时,我希望单元格行改变颜色。我不知道我是如何做到这一点的,但是当我单击“完成”时,整个表格都会更改颜色,除了单元格行。我尝试使用单元原型(prototype)的可重用标识符,将其设置为“单元”,但这没有成功,因为我不确定如何声明“单元”而不干扰我的 CellForRowAt 函数。

这是我的代码:

      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")
}

关于SWIFT:滑动表格单元格以更改其颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51556678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com