如何更改表格视图中特定单元格的编辑样式

How to change editing style for specific cell in tableview

我有一个表格视图,其中所有单元格的编辑样式都是 .delete。但是我想要一些没有编辑样式的特定单元格(这样你就不能滑动它们了,.none)。有人对如何实现这个有任何建议吗?

我尝试为该特定行编写类似 UITableViewCell.EditingStyle.none 的内容,但没有成功。

提前致谢,本都

考虑以下示例:

class ViewController: UITableViewController {

    private let arr = (1...10).map { "Item \([=10=])" }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arr.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        
        cell?.textLabel?.text = arr[indexPath.row]

        return cell ?? UITableViewCell()
    }
    
    override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        if indexPath.row == 2 {//<- Your condition
            return .none
        } else {
            return .delete
        }
    }
    
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
}

所以关键是使用 editingStyleForRowAt 来更改特定单元格(甚至多个单元格)的样式(请参阅文档以获取更多参考:https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614869-tableview)。