gpt4 book ai didi

ios - 按下按钮时更改行的颜色 - swift

转载 作者:搜寻专家 更新时间:2023-11-01 07:05:04 25 4
gpt4 key购买 nike

我有以下代码,当向左滑动 UITableView 行时,会出现两个按钮。

override func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {
let more = UITableViewRowAction(style: .normal, title: "Picked") { action, index in
print("Stock Picked")

}
more.backgroundColor = .green

let favorite = UITableViewRowAction(style: .normal, title: "Not Enough") { action, index in
print("Not enough stock to pick")
}
favorite.backgroundColor = .orange



return [favorite, more]
}

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}

我现在正在努力做到,如果按下其中一个按钮,它所在的行就会改变颜色。即,如果向左滑动一行并按下 Picked 按钮,则该行颜色将变为绿色。

我还希望它在行上设置某种标志,只有当所有行的标志都设置为 true 时,应用程序才能向前移动。

非常感谢任何建议。

最佳答案

要在编辑操作时更改单元格颜色,您应该使用 UITableViewcellForRowAtIndexpath 方法获取单元格,并将背景颜色更改为您想要的颜色,必须这样做UITableViewRowAction

提供的操作 block 内部
override func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {
let more = UITableViewRowAction(style: .normal, title: "Picked") { action, index in
print("Stock Picked")
let cell = tableView.cellForRow(at: index) as? UITableViewCell
cell?.backgroundColor = .green
}
more.backgroundColor = .green

let favorite = UITableViewRowAction(style: .normal, title: "Not Enough") { action, index in
print("Not enough stock to pick")
let cell = tableView.cellForRow(at: index) as? UITableViewCell
cell?.backgroundColor = .orange
}
favorite.backgroundColor = .orange


return [favorite, more]
}

关于ios - 按下按钮时更改行的颜色 - swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48558597/

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