table view 而不是其他人。在这种情况下 section 0 不应该是可删除的(所以也不要滑动以删除),但是 section 1 应该可以。我该如何实现?目前 第0节
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) } } }
UITableView 有一个正是为此目的而调用的方法 canEditRowAt . 当indexPath.section==0时,只需返回false
UITableView
canEditRowAt
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { return indexPath.section != 0 }