我认为,如果有人要运行您的代码,则不会显示任何错误。但是如果有真实数据,它可能会。的 原因 是 存储您的选中标记方式
。您
temp应该在存储数组的实际值时将一行的数据存储
indexPath到数组中,以便 只有该行才具有选中标记
。在您的情况下,如果一行
1内有标签,然后单击它,则该单元格将突出显示。现在,如果您开始滚动并且包含另一个单元格,
1则该行也将突出显示。
对于单节,我已经修改了您的示例。如果有多个部分,则需要存储
indexPath而不是
indexPath.row。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCell(withIdentifier: "id") cell = UITableViewCell.init(style: .default, reuseIdentifier: "id") cell?.textLabel?.text = String(numarr[indexPath.row]) if temp.contains(indexPath.row) { cell?.accessoryType = .checkmark } else { cell?.accessoryType = .none } return cell!}func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let cell = tableView.cellForRow(at: indexPath) if temp.contains(indexPath.row) { cell?.accessoryType = .none temp.remove(at: indexPath.row) } else { cell?.accessoryType = .checkmark temp.append(indexPath.row) }}


