【发布时间】:2018-08-18 07:59:16
【问题描述】:
向上滚动后删除了 TableView 复选标记单元格值它将修复 TableView 中的 TableView 在向上滚动然后向下滚动后多次出现 Checkmark 以显示您的 Checkmark 单元格将被删除,因为单元格是 dequeueReusableCell 所以这个问题修复,您刚刚输入代码并解决了您的问题。
还有更多帮助,请发送按摩。 太感谢了。 :)
class ViewController: UIViewController , UITableViewDataSource , UITableViewDelegate{
var temp = [Int]()
var numarr = [Int]()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numarr.count
}
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(numarr[indexPath.row] as Int)
{
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(numarr[indexPath.row] as Int)
{
cell?.accessoryType = .none
temp.remove(at: temp.index(of: numarr[indexPath.row])!)
}
else
{
cell?.accessoryType = .checkmark
temp.append(self.numarr[indexPath.row] as Int)
}
}
override func viewDidLoad() {
super.viewDidLoad()
for i in 1...100
{
numarr.append(i)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
【问题讨论】:
-
您需要在数据源中更新该项目是否被选中。意味着您的数据集(数组)中的每个项目都必须有一个键值对或属性来指示该项目是否被选中
-
不仅如此 - 您必须实际提出问题,并告诉我们您已经完成的工作。 stackoverflow.com/help/how-to-ask
-
make
tempaSet<IndexPath>add 根据需要添加/删除索引路径
标签: ios swift uitableview