2011-03-28 98 views
15

每当我有我的UITableView中的数据,我开始删除,它工作正常。但是,当我到达表中的最后一个对象并删除它时,它会崩溃。NSInternalInconsistencyException(无效的行数)

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'

以下是我正在做的东西编辑:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     if ([myData count] >= 1) { 
      [tableView beginUpdates]; 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      [myData removeObjectAtIndex:[indexPath row]]; 

      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
      NSString *documentsDirectory = [paths objectAtIndex:0]; 
      NSString *somepath = [documentsDirectory stringByAppendingPathComponent:@"something.plist"]; 
      [myData writeToFile:somepath atomically:YES]; 
      [table reloadData]; 
      if ([myData count] == 0) { 
       [tableView endUpdates]; 
       [tableView reloadData]; 
      } 
      else { 
      [tableView endUpdates]; 
      } 
     } 
    } 
} 

而且也是这样:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    if ([myData count] != 0) { 
     return [myData count]; 
    } 
    else { 
     return 1; 
    } 
} 

我回到1的原因是因为我正在做在cellForRowAtIndexPath中表示“没有数据保存”的单元格。这就是我的意思:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    }  
    if ([cityData count] != 0) { 
     //normal setup removed for clarity 
    } 
    else { 
     cell.textLabel.text = @"No saved data!"; 
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14]; 
    cell.textLabel.textAlignment = UITextAlignmentCenter; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.tag = 1; 
    return cell; 
    } 
} 

那么,我在做错误的编辑代码中得到这个错误?谢谢!

回答

28

如果删除表中的最后一行,则UITableView代码预计剩余0行。它会调用您的方法来确定剩余的数量。由于您有一个“无数据”单元格,它返回1而不是0.因此,当您删除表格中的最后一行时,请尝试调用-insertRowsAtIndexPaths:withRowAnimation:来插入“无数据”行。此外,您不应该在此方法的任何地方拨打-reloadData-endUpdates将负责重新加载受影响的行。试试这个:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     if ([myData count] >= 1) { 
      [tableView beginUpdates]; 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      [myData removeObjectAtIndex:[indexPath row]]; 

      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
      NSString *documentsDirectory = [paths objectAtIndex:0]; 
      NSString *somepath = [documentsDirectory stringByAppendingPathComponent:@"something.plist"]; 
      [myData writeToFile:somepath atomically:YES]; 

      if ([myData count] == 0) { 
       [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      } 
      [tableView endUpdates]; 
     } 
    } 
} 
+1

完美,谢谢! – 2011-03-28 05:00:36

+3

你也可以做的只是调用tableView deleteRowsAtIndexPaths,如果[数据计数]> 0,然后DO调用reloadData。 这样你就不会删除最后一个单元,如果它将被用于“无数据”单元,并且重载会确保委托在桌面视图中加载正确的信息。 – EeKay 2012-08-30 10:09:02

+0

谢谢,这对我也有帮助! – 2013-06-02 18:33:15

1

首先从myData中删除,然后从tableview中删除。

-(void)tableView:(UITableView *)tableView commitEditingStyle: 
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

if (editingStyle == UITableViewCellEditingStyleDelete) { 
     //somehting... 
     [myData removeObjectAtIndex:[indexPath row]]; 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     //somehting... 
    } 
} 

}

1

的方法tableView:numberOfRowsInSection必须始终返回行的确切数字:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    return [myData count]; 
} 

其删除最后一行之后,您可能需要删除整段。只需在beginUpdatesendUpdated区块内拨打deleteSections:withRowAnimation:;

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     [tableView beginUpdates]; 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     [myData removeObjectAtIndex:[indexPath row]]; 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString *somepath = [documentsDirectory stringByAppendingPathComponent:@"something.plist"]; 
     [myData writeToFile:somepath atomically:YES]; 
     if ([myData count] == 0) { 
      // NEW! DELETE SECTION IF NO MORE ROWS! 
      [tableView deleteSections:[NSIndexSet indexSetWithIndex:[indexPath section]] withRowAnimation:UITableViewRowAnimationFade]; 
     } 
     [tableView endUpdates]; 
    } 
} 
相关问题