【问题标题】:Issue Editing Mode by swiped cell in tableView通过 tableView 中的滑动单元格发布编辑模式
【发布时间】:2021-10-21 09:33:17
【问题描述】:

我有以下问题。如果我只滑动单元格直到删除按钮可见,然后单击编辑,则表格未处于编辑模式,但按钮显示它。

我已经使用shouldBeginEditingAtRow 和didEndEditingAtRow 方法看到,如果我先滑动一行直到删除按钮,然后再滑动下一行,tableView.isEditing 的状态为true。 这意味着如果我滑动一行直到删除按钮可见,然后单击编辑,setEditing 会将 tableView 的状态设置为false。

我已经尝试在didEndEditingAtRow 方法中将状态设置为false。但不幸的是,这不起作用。你有什么建议或解决方案吗?

我的代码:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    var data = [1,2,3,4,5,6,7,8,9]
    
    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(!isEditing, animated: true)
        tableView.setEditing(!tableView.isEditing, animated: true)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = String(data[indexPath.row])
        return cell
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            data.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .automatic)
        }
    }

    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        tableView.dataSource = self
        tableView.delegate = self
        
        
        navigationItem.rightBarButtonItem = editButtonItem
    }


}

【问题讨论】:

    标签: ios swift xcode uitableview editing


    【解决方案1】:

    首先,当您覆盖 UIKit 方法时,请尝试从您要覆盖的方法中传递参数,因此请使用以下几行更新 setEditing 方法:

       override func setEditing(_ editing: Bool, animated: Bool) {
             super.setEditing(editing, animated: true)
                 //handle Animation
                 if editing  {
                   self.tableView.reloadSections(IndexSet(integersIn: 0...0), with: .none)
                 }
             tableView.setEditing(editing, animated: true)
     }
    

    【讨论】:

    • 嗨,我试过你的代码,它可以工作,但是当表格进入和退出编辑模式时会导致执行的动画不能顺利执行。很可能是由于setEditing() 中的tableView.reloadData()。所以我对解决方案还不完全满意。
    • @anuri 我已经更新了答案,你应该得到你想要的。
    猜你喜欢
    • 1970-01-01
    • 2015-10-02
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 2013-11-29
    • 2017-04-18
    相关资源
    最近更新 更多