You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

UITableView无法切换至编辑模式并恢复常态的问题求助

Hey there, let's troubleshoot why your UITableView isn't entering edit mode as expected. Looking over your code, there are two key issues that are blocking this functionality:

1. Hardcoded editing state in setEditing

In your setEditing(_:animated:) method, you're setting tableView.isEditing = true no matter what the incoming editing parameter is. This means when you tap the "Edit" button, the table might enter edit mode, but tapping "Done" won't toggle it back off—plus it breaks the core state sync with the navigation bar button.

Fix this by tying the table's editing state directly to the method's parameter, and use the table's built-in setEditing(_:animated:) to keep animations in sync with the system:

override func setEditing(_ editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)
    inboxViewComponent.tableView.setEditing(editing, animated: animated)
}

2. Missing edit permission methods in the table view delegate/data source

UITableView doesn't allow row editing by default—you need explicitly enable it via data source methods. Since your InboxViewComponent acts as the table's delegate and data source, add these methods to it:

extension InboxViewComponent: UITableViewDataSource, UITableViewDelegate {
    // (Assuming you already have the basic data source methods like numberOfRowsInSection, cellForRowAt, etc.)

    // Allow rows to be edited
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    // Optional: Define the editing style (default is delete if omitted, but explicit is clearer)
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return .delete
    }
}

After making these two changes, your table view should properly toggle edit mode when you tap the navigation bar's edit/done button, and show the expected editing controls (like delete buttons) for each row.

内容的提问来源于stack exchange,提问作者bruno

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 14:27:39