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

使用setEditing无法使第二个TableView进入编辑模式,求助解决方案

同一行移动代码在第二个TableView无法触发编辑模式,求帮忙排查

我在第一个TableView里用下面这段代码实现行移动功能,运行完全正常——点击按钮就能进入编辑模式,拖拽行也没问题。但把一模一样的代码放到第二个TableView里就不行了:按钮点击事件能触发(我加了日志确认),但TableView就是死活进不了编辑模式。

我已经试过不少常见的解决方案了,比如检查代理是否设置、确认tableView.isEditing的赋值逻辑、甚至对比了两个TableView的所有属性配置,但还是找不到问题出在哪。有没有大佬能帮我想想还可能遗漏了什么细节?

我的核心代码片段:

@IBAction func toggleEditMode(_ sender: UIButton) {
    tableView.setEditing(!tableView.isEditing, animated: true)
    sender.setTitle(tableView.isEditing ? "完成" : "编辑", for: .normal)
}

// MARK: - UITableViewDelegate & DataSource
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
    return true
}

override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    // 行移动后的数据源更新逻辑,两个TableView里这段逻辑也是一致的
    let movedItem = dataArray.remove(at: sourceIndexPath.row)
    dataArray.insert(movedItem, at: destinationIndexPath.row)
    tableView.reloadData()
}

已经排查过的点:

  • 确认第二个TableView的delegate和dataSource都正确关联到了对应的ViewController
  • 按钮的IBAction已经正确绑定,点击时tableView.setEditing(_:animated:)确实被调用了
  • 检查了两个TableView的Storyboard属性,Editing相关的设置都是默认状态
  • 没有在其他地方覆盖tableView.isEditing的取值

真的快挠头了,明明代码完全一样,为啥第二个TableView就是不配合😩

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:36:51