【发布时间】:2018-06-29 01:08:33
【问题描述】:
正在开发具有 tableview 的应用程序。在每个单元格中都有两个按钮(通过/失败)。如果用户点击“ok”,通过背景变为绿色,点击“!”失败,背景变为红色。我遇到的问题是,如果连续点击一个按钮(比如说失败按钮)。滚动到另一行(比如向下 5 行)。失败按钮的背景颜色也改变了(红色)。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell: CellTableViewCell! = tableView.dequeueReusableCell(withIdentifier: "cell") as! CellTableViewCell
if cell == nil
{
cell = CellTableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = myArray[indexPath.row]
cell.fail.tag = indexPath.row
cell.fail.addTarget(self, action: #selector(ViewController.errorBtn(_:)), for: .touchUpInside)
}
else
{
cell.textLabel?.text = myArray[indexPath.row]
cell.fail.tag = indexPath.row
cell.fail.addTarget(self, action: #selector(ViewController.errorBtn(_:)), for: .touchUpInside)
}
return cell
}
这里有一个链接到应用程序的快速模型,我的代码有问题。 https://github.com/morenoa/iOS3/tree/master/Table%20Cell 任何帮助是极大的赞赏 谢谢。 抱歉,如果重新发布。只是被难住了。
【问题讨论】:
-
您应该检查按钮是否已在 cellForRowAt 中被选中,然后相应地更改背景颜色。
标签: ios swift uibutton tableview