【问题标题】:TableView CheckMark and Uncheck With Scroll Up Still Checked Cell Value In Ios Swift 4TableView CheckMark 和 Uncheck With Scroll Up 在 Ios Swift 4 中仍然检查单元格值
【发布时间】: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 temp a Set<IndexPath> add 根据需要添加/删除索引路径

标签: ios swift uitableview


【解决方案1】:

我认为如果有人运行您的代码,它不会显示任何错误。但如果有真实数据,它可能会。 原因您存储复选标记的方式。当您应该存储数组的实际indexPath 时,您将一行的数据存储到temp 数组中,以便只有该行获得复选标记。在您的情况下,如果一行的标签中有 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)
    }
}

【讨论】:

    【解决方案2】:

    强烈建议您不要使用第二个数组来保持选定状态。

    这是 Swift,一种面向对象的语言。为 num 和选定状态使用自定义结构。
    didSelectRowAtdidDeselectRowAt 中更改isSelected 的值并重新加载行。

    并始终使用返回非可选单元格的dequeueReusableCell API。

    struct Item {
       let num : Int
       var isSelected : Bool
    }
    
    var numarr = [Item]()
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return numarr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "id", for: indexPath)
        let item = numarr[indexPath.row]
        cell.textLabel?.text = String(item)
        cell.accessoryType = item.isSelected ? .checkmark : .none
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        updateSelection(at: indexPath, value : true)
    }
    
    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        updateSelection(at: indexPath, value : false)
    }
    
    func updateSelection(at indexPath: IndexPath, value : Bool) {
        let item = numarr[indexPath.row]
        item.isSelected = value
        tableView.reloadRows(at: [indexPath], with: .none)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        (0...100).map{Item(num: $0, isSelected: false)}
    }
    

    【讨论】:

      猜你喜欢
      • 2019-02-20
      • 2014-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-21
      • 1970-01-01
      相关资源
      最近更新 更多