1

I want to stop all the rows being deleted from my table view by user so at least one row is always present.

Im still geting to grips with programming and objective c.

Heres what I tried:

  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];

then

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];

That had no effect in that I can still delete the first row. So then I tried

- (BOOL)tableView:(UITableView *)commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) 
    return NO;

return YES;
}

Still cant prevent the first row from being deleted

4

3 に答える 3

4

UITableViewDataSourceメソッドを実装し、tableView:canEditRowAtIndexPath:を返す必要がNOありindexPath.row 0ます。

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return indexPath.row > 0;
}
于 2013-01-04T15:04:45.860 に答える
2

コードを移動してみてください - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

于 2013-01-04T15:04:38.420 に答える
0

このコードを試してください:-

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
    if(indexPath.row==0)
    return NO;
    else
    return YES;

    }

きっとお役に立ちます

于 2013-06-12T11:44:04.997 に答える