代码之家  ›  专栏  ›  技术社区  ›  Nouman

不要从UITableView中删除某些行

  •  0
  • Nouman  · 技术社区  · 5 年前

    table view 而不是其他人。在这种情况下 section 0 不应该是可删除的(所以也不要滑动以删除),但是 section 1 应该可以。我该如何实现?目前 第0节

    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)
                }
            }
    
        }
    
    1 回复  |  直到 5 年前
        1
  •  2
  •   Leo Dabus    5 年前

    UITableView 有一个正是为此目的而调用的方法 canEditRowAt . 当indexPath.section==0时,只需返回false

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