在UITableView中删除了一行/单元格,但删除的行仍显示在屏幕上

在UITableView中删除了一行/单元格,但删除的行仍显示在屏幕上,uitableview,Uitableview,我根据iPhone开发者的食谱C5-表07a-删除编写了以下代码片段: -(void)viewDidLoad{ history=[[NSMutableArray alloc]initWithObjects:@"a",@"b",@"c",@"d",@"e",nil]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ NSLog(@"return %d

我根据iPhone开发者的食谱C5-表07a-删除编写了以下代码片段:

-(void)viewDidLoad{
    history=[[NSMutableArray alloc]initWithObjects:@"a",@"b",@"c",@"d",@"e",nil];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"return %d",[history count]);
    return [history count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[self.tableView dequeueReusableCellWithIdentifier:@"any-cell"];
    if(cell==nil)
        cell=[[[UITableViewCell alloc]initWithFrame:CGRectZero reuseIdentifier:@"any-cell"]autorelease];
    cell.textLabel.text=[history objectAtIndex:[indexPath row]];
    cell.textLabel.textColor=[UIColor blackColor];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"[history count]=%d",[history count]);
    NSLog(@"[indexPath row]=%d",[indexPath row]);
    [history removeObjectAtIndex:[indexPath row]];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
建好后,我按了导航栏上的“编辑”按钮,显示一个红色“-”表示删除。但屏幕上什么也没变。在多次点击“-”后,应用程序崩溃。输出为:

> 2011-07-13 05:16:34.128 test[13141:207] return 5 2011-07-13
> 05:16:37.909 test[13141:207] [history count]=5 2011-07-13 05:16:37.910
> test[13141:207] [indexPath row]=0 2011-07-13 05:16:39.069
> test[13141:207] [history count]=4 2011-07-13 05:16:39.070
> test[13141:207] [indexPath row]=0 2011-07-13 05:16:39.653
> test[13141:207] [history count]=3 2011-07-13 05:16:39.654
> test[13141:207] [indexPath row]=0 2011-07-13 05:16:40.069
> test[13141:207] [history count]=2 2011-07-13 05:16:40.070
> test[13141:207] [indexPath row]=0 2011-07-13 05:16:40.509
> test[13141:207] [history count]=1 2011-07-13 05:16:40.510
> test[13141:207] [indexPath row]=0 2011-07-13 05:16:41.581
> test[13141:207] [history count]=0 2011-07-13 05:16:41.582
> test[13141:207] [indexPath row]=0 2011-07-13 05:16:41.589
> test[13141:207] *** Terminating app due to uncaught exception
> 'NSRangeException', reason: '*** -[NSMutableArray
> removeObjectAtIndex:]: index 0 beyond bounds for empty array'
> *** Call stack at first throw: (
>     0   CoreFoundation                      0x00db3be9 __exceptionPreprocess + 185
>     1   libobjc.A.dylib                     0x00f085c2 objc_exception_throw + 47
>     2   CoreFoundation                      0x00dacadf -[__NSArrayM removeObjectAtIndex:] + 415
>     3   Â∞èËaæáßò‰π¶                        0x00004614 -[CallHistory tableView:commitEditingStyle:forRowAtIndexPath:] + 182
>     4   UIKit                               0x003262ea -[UITableView(UITableViewInternal) animateDeletionOfRowWithCell:] + 101
>     5   UIKit                               0x002bca6e -[UIApplication sendAction:to:from:forEvent:] + 119
>     6   UIKit                               0x0034b1b5 -[UIControl sendAction:to:forEvent:] + 67
>     7   UIKit                               0x0034d647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
>     8   UIKit                               0x0034c1f4 -[UIControl touchesEnded:withEvent:] + 458
>     9   UIKit                               0x002e10d1 -[UIWindow _sendTouchesForEvent:] + 567
>     10  UIKit                               0x002c237a -[UIApplication sendEvent:] + 447
>     11  UIKit                               0x002c7732 _UIApplicationHandleEvent + 7576
>     12  GraphicsServices                    0x016e9a36 PurpleEventCallback + 1550
>     13  CoreFoundation                      0x00d95064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
>     14  CoreFoundation                      0x00cf56f7 __CFRunLoopDoSource1 + 215
>     15  CoreFoundation                      0x00cf2983 __CFRunLoopRun + 979
>     16  CoreFoundation                      0x00cf2240 CFRunLoopRunSpecific + 208
>     17  CoreFoundation                      0x00cf2161 CFRunLoopRunInMode + 97
>     18  GraphicsServices                    0x016e8268 GSEventRunModal + 217
>     19  GraphicsServices                    0x016e832d GSEventRun + 115
>     20  UIKit                               0x002cb42e UIApplicationMain + 1160
>     21  Â∞èËaæáßò‰π¶                        0x0000229c main + 106
>     22  Â∞èËaæáßò‰π¶                        0x00002229 start + 53
>     23  ???                                 0x00000001 0x0 + 1 ) 
terminate called after throwing an instance of 'NSException'
NSLog
显示历史记录(这是一个
NSMutableArray
)实际上已经更改。我构建了iPhone开发者的食谱C5-Tables 07a-Delete,发现发生了类似的崩溃,tableView:numberOfRowsInSection:只被调用了一次。但是
DeleteMeController
在iPhone3开发09开始时,Nav成功地删除了一行,并且每次调用
tableView:numberOfRowsInSection:
时都会调用
tableView:committeeditingstyle:forrowatinexpath:
。iPhone开发者的菜谱代码是手工编写的,在iPhone 3开发之初使用Interface Builder


有什么不对劲的线索吗?

酷。。。我有了新的解决办法。用于显示编辑样式

-(void)tableView:(UITableView *)tableView commitEditingStyle: UITableViewCellEditingStyle) editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        if (editingStyle == UITableViewCellEditingStyleDelete) { 
                [history removeObjectAtIndex:indexPath.row];
                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
        [tableView endUpdates];
        [tableView reloadData];
       }
}
if(tableView.isEditing){
    [tableView setEditing:FALSE animated:YES];
    [tableView setNeedsLayout];
}else{
    [tableView setEditing:TRUE animated:YES];        
    [tableView setNeedsLayout];
}

是的,有[tableView EndUpdate];[tableView重新加载数据];成功了!谢谢你,卡尔佩什!啊。。。你救了我的命!谢谢你的帮助。:)我很高兴能帮助你们。