代码之家  ›  专栏  ›  技术社区  ›  John Montgomery

删除行后无法禁用对表的编辑

  •  1
  • John Montgomery  · 技术社区  · 7 年前

    setEditing

    class TestViewController: UITableViewController {
        var items = [String]()
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.navigationItem.rightBarButtonItems = [
                UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(add)),
                UIBarButtonItem(title: "Remove", style: .plain, target: self, action: #selector(remove))
            ]
        }
    
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return self.items.count
        }
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = UITableViewCell()
            cell.textLabel?.text = self.items[indexPath.row]
            return cell
        }
        override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
            self.items.remove(at: indexPath.row)
            if self.items.count == 0 {
                // This is getting executed, but not changing the table's state
                self.tableView.setEditing(false, animated: false)
            }
            self.tableView.reloadData()
        }
    
        @objc func add () {
            self.items.append("foo")
            self.tableView.reloadData()
        }
        @objc func remove () {
            self.tableView.setEditing(!self.tableView.isEditing, animated: true)
        }
    }
    

    单击“删除”按钮可以打开和关闭编辑,没有任何问题,但是如果我删除了最后一个,然后添加了一个新的,它仍然会在编辑模式下显示。我尝试过单步执行代码,它肯定是正确的,但即使在执行之后 tableView.isEditing

    2 回复  |  直到 7 年前
        1
  •  1
  •   Saurabh Prajapati    7 年前

    我已经检查了你的代码,根据渡边的评论,它不工作。 所以我改变了我的想法 方法和它工作良好, 请检查下面的代码

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
            self.items.remove(at: indexPath.row)
            self.tableView.deleteRows(at: [indexPath], with: .left)
              if self.items.count == 0 {
                 // This is getting executed, but not changing the table's state
                 self.tableView.setEditing(false, animated: false)
            }
            //self.tableView.reloadData()
     }
    

    更新后约翰的评论-

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
            self.items.remove(at: indexPath.row)
            if self.items.count == 0 {
                // This is getting executed, but not changing the table's state
                self.tableView.deleteRows(at: [indexPath], with: .none)
                self.tableView.setEditing(false, animated: false)
            }else{
                self.tableView.reloadData()
            }
        }
    
        2
  •  0
  •   nitin.agam    7 年前

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return editingEnable
    }