不要从 UITableView 中删除一些行 [英] Don't delete some rows from UITableView

查看:33
本文介绍了不要从 UITableView 中删除一些行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现从 table view 中删除某些行而不是其他行的功能.在这种情况下,section 0 中的所有内容都不应该是可删除的(所以也不要滑动删除),但是 section 1 中的所有内容都应该可以.我该如何实施?目前section 0行无法删除,但是当用户滑动时,仍然出现删除动作.

I'm trying to implement the functionality to delete some rows from a table view and not others. In this case, everything in section 0 should not be deletable (so not swipe to delete either), but everything in section 1 should be able to. How can I implement this? Currently section 0 rows cannot delete, but when the user swipes, the delete action still appears.

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {

    if (indexPath.section == 0){
        // dont delete the rows

    } else {
        if (editingStyle == .delete){
            let fetchRequest: NSFetchRequest<Conversions> = Conversions.fetchRequest()
            do {
                let result = try PersistenceService.context.fetch(fetchRequest)

                // Delete from CoreData and remove from the array
                if (result.contains(allConversions[indexPath.row])){
                    PersistenceService.context.delete(allConversions[indexPath.row])
                    allConversions = allConversions.filter { $0 != allConversions[indexPath.row] }
                    PersistenceService.saveContext()
                    self.tableView.reloadData()
                }
            } catch {
                print(error.localizedDescription)
            }
        }

    }

推荐答案

UITableView 有一个专门用于此目的的方法,称为 canEditRowAt.您只需要在 indexPath.section == 0

UITableView has a method exactly for this purpose called canEditRowAt. You just need to return false when indexPath.section == 0

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 
    return indexPath.section != 0 
}

这篇关于不要从 UITableView 中删除一些行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆