繁体   English   中英

滚动tableview导致复选标记消失 解决Swift 5中的可重用单元

[英]Scrolling tableview causes checkmark to disappear | addressing reusable cells in Swift 5

我使用以下内容将表视图中的行标记为带有复选标记或未选中且没有复选标记的行。 我偶然发现的问题是在滚动tableView时似乎重新加载并导致复选标记消失。

我了解这是由可重复使用的单元格引起的,是否可以在以下代码中实现一个简单的修复程序?

class CheckableTableViewCell: UITableViewCell {
    var handler: ((Bool)->())?

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        self.accessoryType = selected ? .checkmark : .none
        handler?(selected)
    }
}


class TableViewController: UITableViewController {


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CheckableTableViewCell
        cell.handler = {[weak self](selected) in
            selected ? self?.selectRow(indexPath) : self?.unselectRow(indexPath)
        }
                let section = sections[indexPath.section]
                let item = section.items[indexPath.row]
                cell.textLabel?.textAlignment = .left
                cell.selectionStyle = .none

                let stringText = "\(item.code)"
                cell.textLabel!.text = stringText


        return cell
    }

更新:

struct Section {
    let name : String
    var items : [Portfolios]
}

struct Portfolios: Decodable {

    let code: String
    var isSelected: Bool

    enum CodingKeys : String, CodingKey {
        case code
    }

}

您需要创建一个具有名为isSelected: Bool的属性的数据模型isSelected: Bool然后使用它来决定何时应选择行。 请注意,每次触发didSelectRowAt(indexPath:)时,都必须切换此属性。


// Declare your model with one of the property called isSelected

class MyModel {

var isSelected: Bool

init(isSelected: Bool) {
self.isSelected = isSelected
}
}


class TableViewController: UITableViewController {

// Dummy data note that it contains array of "MyModel" which have the "isSelected" property. 
private var myModels: [MyModel] = [MyModel(isSelected: false),MyModel(isSelected: true),MyModel(isSelected: true),MyModel(isSelected: false) ]

   override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CheckableTableViewCell
       cell.handler = {[weak self](selected) in

            /// Note that I am using selected Property here
           myModels[indexPath.row].isSelected ? self?.selectRow(indexPath) : self?.unselectRow(indexPath)
       }
               let section = sections[indexPath.section]
               let item = section.items[indexPath.row]
               cell.textLabel?.textAlignment = .left
               cell.selectionStyle = .none

               let stringText = "\(item.code)"
               cell.textLabel!.text = stringText


       return cell


  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
   let selectedIndexPath = indexPath
   myModels[selectedIndexPath.row].isSelected = true
   self.tableView.reloadRows(at: [selectedIndexPath], with: .none)

   }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM