我想阻止用户从我的 TableView 中删除所有行,以便始终存在至少一行。
我仍然在掌握编程和 objective-c 。
这是我尝试过的:
if (indexPath.section != 0 | editingStyle == UITableViewCellEditingStyleDelete) {
[self.managedObjectContext deleteObject:[self.circuits objectAtIndex:indexPath.row]];
[self.appDelegate saveContext];
[self.circuits removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
然后
if (indexPath.section > 1 | editingStyle == UITableViewCellEditingStyleDelete) {
[self.managedObjectContext deleteObject:[self.circuits objectAtIndex:indexPath.row]];
[self.appDelegate saveContext];
[self.circuits removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
这没有任何影响,因为我仍然可以删除第一行。所以我尝试了
- (BOOL)tableView:(UITableView *)commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0)
return NO;
return YES;
}
还是不能防止第一行被删除
最佳答案
您需要执行 UITableViewDataSource
方法tableView:canEditRowAtIndexPath:
并返回 NO
对于indexPath.row
0
.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath.row > 0;
}
关于objective-c - 防止在 TableView 中删除第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14159527/