我有以下代码,当 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
}
我现在正在尝试这样做,以便如果按下其中一个按钮,它所显示的行就会改变颜色。即,如果向左滑动一行并按下“已选择”按钮,则行颜色将变为绿色。
我还希望它在行上设置某种标志,只有当所有行的标志设置为 true 时,才允许应用程序向前移动。
非常感谢任何建议。
最佳答案
要在编辑操作中更改单元格颜色,您应该使用 UITableView
的 cellForRowAtIndexpath
方法获取单元格,并将背景颜色更改为您想要的颜色,必须完成此操作UITableViewRowAction
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 - 按下按钮时更改行的颜色 - 快速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48558597/