Iphone 使用CommittedItingStyle删除后没有更新或numberOfRowsInSection

Iphone 使用CommittedItingStyle删除后没有更新或numberOfRowsInSection,iphone,xcode,uitableview,Iphone,Xcode,Uitableview,使用编辑/删除或滑动删除手势删除项目后,我的表格不会更新。如果我编辑/删除,删除按钮不会消失。它保持在按下/卡住状态。然后单击“完成”时,“删除”按钮消失。这篇文章的底部附有一个截图 My numberOfRowsInSection代码在应用程序启动时执行,但不会在删除项目后执行 我正在使用UITableViewController。my.h文件中的定义: @interface BNVFavoritesTableViewController : UITableViewController <

使用编辑/删除或滑动删除手势删除项目后,我的表格不会更新。如果我编辑/删除,删除按钮不会消失。它保持在按下/卡住状态。然后单击“完成”时,“删除”按钮消失。这篇文章的底部附有一个截图

My numberOfRowsInSection代码在应用程序启动时执行,但不会在删除项目后执行

我正在使用UITableViewController。my.h文件中的定义:

@interface BNVFavoritesTableViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>{    
  BNVAppDelegate *appDelegate;    
  IBOutlet UITableView *myTable;
}

@property (retain, nonatomic) IBOutlet UITableView *myTable;
@end
删除时,我会在日志中看到“删除前”和“删除后”消息(如预期),但不会看到“行数”消息

表视图中的数据源和代理出口连接到TableViewController类。我也试着在ViewDidLoad方法中这样做,但这并没有造成什么不同。强制执行
[myTable重载数据]committeditingstyle
末尾的code>也没有帮助

最后,这里是“卡住”删除按钮的屏幕截图。


单击几次会导致NSRangeException/越界错误,表明执行了
committeeditingstyle
中的代码。

您需要在
beginUpdates
endUpdates
之间添加删除动画

[myTable beginUpdates];
[myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[myTable endUpdates];

只需在表的连接检查器中检查连接即可

或者如果连接正确,那么 删除记录后重新加载表

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // If row is deleted, remove it from the list.
    if (editingStyle == UITableViewCellEditingStyleDelete){
        NSLog(@"before delete\t%d", [appDelegate.favoritesArray count]);        
        BNVFavorite *selectedObject = [appDelegate.favoritesArray objectAtIndex:indexPath.row];
        NSString *selectedName =  selectedObject.name;
        // delete from sqlite database
        [appDelegate removeFavoriteFromDB:selectedName];
        // delete from memory array
        [appDelegate.favoritesArray removeObjectAtIndex:indexPath.row];
        // delete in user interface
        [myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        NSLog(@"after delete\t%d", [appDelegate.favoritesArray count]);
        [myTable reloadData];
    }

}

经过几个小时的搜索,我发现了这个问题。事实证明,从故事板中的tableview到控制器类中的myTable的引用出口不再存在。我一定是不小心把它拿走了。当我将
myTable
替换为
self.tableView

时,我注意到代码开始工作。嘿,在appDelegate.favoritesArray中,对象是否被删除?是的,它被删除了。如果我多次单击“删除”按钮,就会出现NSRangeException错误,正如预期的那样。您的行是否从表中删除了..?是的,但这不是问题所在。我在回答中提到了您的答案..。)遗憾的是,这没有帮助。似乎没有什么不同。谢谢你的帮助!已经尝试过:(强制[myTable重载数据];在我的提交结束时,Style没有帮助。好的..可能是您在数据库中没有正确删除数据…只需检查您的数据库否,也不是这样:数据已正确删除。我昨天找到了解决方案,并已发布了答案。问题是我的Tableview outlet不再正确连接到我的类。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // If row is deleted, remove it from the list.
    if (editingStyle == UITableViewCellEditingStyleDelete){
        NSLog(@"before delete\t%d", [appDelegate.favoritesArray count]);        
        BNVFavorite *selectedObject = [appDelegate.favoritesArray objectAtIndex:indexPath.row];
        NSString *selectedName =  selectedObject.name;
        // delete from sqlite database
        [appDelegate removeFavoriteFromDB:selectedName];
        // delete from memory array
        [appDelegate.favoritesArray removeObjectAtIndex:indexPath.row];
        // delete in user interface
        [myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        NSLog(@"after delete\t%d", [appDelegate.favoritesArray count]);
        [myTable reloadData];
    }

}