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

设置editing为true时,leadingSwipeActionsConfigurationForRowAt失效求助

UITableView左滑删除与拖拽排序共存问题

需求:实现UITableView,支持左滑(leading swipe)删除行,同时显示汉堡图标支持拖动调整行顺序。单独使用左滑删除或拖拽排序均正常,但同时通过setEditing(true)开启拖拽排序后,左滑动作被禁用。尝试过调整tableView的allow系列属性、实现canEditRowAt方法,均未解决该问题。

代码片段:

import UIKit

class CScreensTableViewController: UITableViewController, UIPopoverPresentationControllerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.setEditing(true, animated: true)
        tableView.allowsSelectionDuringEditing = true
        
        Model.shared.retrieveData()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        tableView.reloadData()
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return createHeaderView(title: NSLocalizedString("metrics", comment: "Metrics"))
    }
    
    override func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
        return false
    }
    
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 96
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return Model.shared.getNrOfEntries()
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // 为简化问题,此处省略单元格创建代码,无tableView相关调用
    }
    
    override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return UITableViewCell.EditingStyle.none
    }
    
    override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        if sourceIndexPath.row != destinationIndexPath.row {
            Model.shared.moveScreen(from: sourceIndexPath.row, to: destinationIndexPath.row)
        }
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        model.didSelect(atIndex: indexPath.row)
    }
    
    override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let action: UIContextualAction = UIContextualAction(style: .normal, title: nil) { (_, _, completionHandler) in
            if Model.shared.tryDelete(withIndex: indexPath.row) {
                self.tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.middle)
            }
            completionHandler(true)
        }
        
        action.image = UIImage(systemName: "trash.fill", withConfiguration: UIImage.SymbolConfiguration(weight: .bold))?.withTintColor(.black, renderingMode: .alwaysOriginal)
        action.backgroundColor = UIColor.red
        return UISwipeActionsConfiguration(actions: [action])
    }
}

疑问:使用UITableView标准组件无法实现该需求吗?还是遗漏了某个关键配置?


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 07:45:53