我有 UITableView 单元格用于显示 UITextView 文本。我需要使用删除一些单元格行文本UITableViewCellEditingStyleDelete
。当我使用编辑按钮删除一些文本时,我得到了错误。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSMutableArray* myMutableArrayAgain = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"save"]];
// NSLog(@"array is %@",myMutableArrayAgain);
return [myMutableArrayAgain count];
}
删除功能:
-(void)editButtonPressed:(UIButton *)button{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
[tableView setEditing:![tableView isEditing] animated:YES];
NSString *buttonTitle = ([tableView isEditing]) ? @"Done" : @"Edit";
[editButton setTitle:buttonTitle forState:UIControlStateNormal];
NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}
- (void)tableView:(UITableView *)tableView1
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
NSMutableArray* myMutableArrayAgain = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"save"]];
if (editingStyle == UITableViewCellEditingStyleDelete)
{
//first delete this from the db of favorites table
NSLog(@"art id is %@",[myMutableArrayAgain objectAtIndex:indexPath.row]);
NSLog(@"indexpath is %d", indexPath.row);
[myMutableArrayAgain removeObjectAtIndex:indexPath.row];
NSLog(@"remove %d",indexPath.row);
NSArray *indexPathsToRemove = [NSArray arrayWithObject:indexPath];
NSLog(@"indextoremove %@",indexPathsToRemove);
[tableView1 deleteRowsAtIndexPaths:indexPathsToRemove withRowAnimation:UITableViewRowAnimationRight];
}
NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}
- (void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath
{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
NSMutableArray* myMutableArrayAgain = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"save"]];
NSString *contentsToMove = [[myMutableArrayAgain objectAtIndex:[fromIndexPath row]] retain];
[myMutableArrayAgain removeObjectAtIndex:[fromIndexPath row]];
[myMutableArrayAgain insertObject:contentsToMove atIndex:[toIndexPath row]];
[contentsToMove release];
NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}
Nslog 中的错误:
2013-10-07 14:29:41.939 WorkSa[3689:c07] indexpath is 0
2013-10-07 14:29:41.940 WorkSa[3689:c07] remove 0
2013-10-07 14:29:41.940 WorkSa[3689:c07] indextoremove (
"<NSIndexPath 0x8b44fa0> 2 indexes [0, 0]"
)
2013-10-07 14:29:41.941 WorkSa[3689:c07] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1046
2013-10-07 14:29:41.942 WorkSafeACT[3689:c07] *** 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 (7) must be equal to the number of rows contained in that section before the update (7), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'