【问题标题】:TableViewCell deleted then All checkmarks disappear on swiftUITableViewCell 删除然后所有复选标记在swift上消失
【发布时间】:2018-10-23 10:42:03
【问题描述】:

我在网上搜索了我的问题,但对我的情况没有任何帮助。有点特别。

我的应用程序有一个清单功能,一切正常,但有这个问题。在UITableViewCell 上添加checkmark 并在此之后删除其他单元格之一。所有checkmarks 将被删除。

我的想法是,当某些内容被删除时,数组和连接会出现问题。我的意思是数组中属性的“顺序”。我问过我的同事 (IT),但没有人能帮助我。

class ViewControllerChecklist: UIViewController, UITableViewDelegate, UITableViewDataSource{

    @IBOutlet weak var myTableView: UITableView!

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return (checklist.count)
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
        cell.textLabel?.textColor = UIColor(red: 4 / 255.0, green: 59 / 255.0, blue: 101 / 255.0, alpha: 1.0)
        cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 18.0)
        cell.textLabel?.text = checklist[indexPath.row]

        return cell
    }

    // checkmarks when tapped
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark

        tableView.deselectRow(at: indexPath, animated: true)
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == UITableViewCellEditingStyle.delete {
            checklist.remove(at: indexPath.row)
            myTableView.reloadData()
        }
    }

    override func viewDidAppear(_ animated: Bool) {
        myTableView.reloadData()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        UITableViewCell.appearance().tintColor = UIColor(red: 237 / 255.0, green: 108 / 255.0, blue: 4 / 255.0, alpha: 1.0)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

选定的单元格

点击删除

单元格已删除

【问题讨论】:

    标签: arrays swift uitableview


    【解决方案1】:

    您可以试用此代码。

    存储选定的值并在UITableViewreloadData() 时间进行比较。

    class ViewControllerChecklist: UIViewController, UITableViewDelegate, UITableViewDataSource{
    
        @IBOutlet weak var myTableView: UITableView!
    
        var selectedChecklist: [String] = []
    
        public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
            return (checklist.count)
        }
    
        public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
            let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
            cell.textLabel?.textColor = UIColor(red: 4 / 255.0, green: 59 / 255.0, blue: 101 / 255.0, alpha: 1.0)
            cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 18.0)
            cell.textLabel?.text = checklist[indexPath.row]
    
            if selectedChecklist.contains(checklist[indexPath.row]) {
                cell.accessoryType = .checkmark
            }
            else{
                cell.accessoryType = .none
            }
    
            return cell
        }
    
        // checkmarks when tapped
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
            tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
            selectedChecklist.append(checklist[indexPath.row])
            tableView.deselectRow(at: indexPath, animated: true)
        }
    
        func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
            if editingStyle == .delete {
                let value = checklist.remove(at: indexPath.row)
                myTableView.reloadData()
            }
        }
    
        override func viewDidAppear(_ animated: Bool) {
            myTableView.reloadData()
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            UITableViewCell.appearance().tintColor = UIColor(red: 237 / 255.0, green: 108 / 255.0, blue: 4 / 255.0, alpha: 1.0)
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    }
    

    【讨论】:

      【解决方案2】:

      更简单的解决方案是让您的对象清单列表在您的清单中添加isChecked。

      struct Object {
          var tag:String
          var isChecked:Bool
      }
      

      然后制作这样的列表:

      var checkList = [Object]()
      

      终于在 cellForRowAt 函数中:

      public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      
          //  rest of code goes here...
      
          if checklist[indexPath.row].isChecked {
              cell.accessoryType = .checkmark
          }
          else{
              cell.accessoryType = .none
          }
      
          return cell
      }
      
      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      
          checkList[indexPath.row].isChecked = true
          tableView.deselectRow(at: indexPath, animated: true)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-11
        • 2014-09-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多