2013-08-20 83 views
3

我正在使用fmdb来管理常规UITableView上显示的一些数据。我试图用这个代码来删除一个单元:从UITableView删除UITableViewCell

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     db = [FMDatabase databaseWithPath:[Utility getDatabasePath]]; 


     [db open]; 

     [db beginTransaction]; 

     NSString * stringtoInsert = [NSString stringWithFormat: @"DELETE FROM TTLogObject WHERE id='%@'", [idArray objectAtIndex:indexPath.row]]; 

     BOOL success = [db executeUpdate:stringtoInsert]; 

     if (!success) 
     { 
      NSLog(@"insert failed!!"); 
     } 

     NSLog(@"Error %d: %@", [db lastErrorCode], [db lastErrorMessage]); 

     [db commit]; 

     [db close]; 

     [self getList]; 
    } 
} 

这里是viewDidLoad中的代码和我使用的GetList功能。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.navigationController.navigationBar.tintColor = [UIColor blackColor]; 

    shipperCityArray = [[NSMutableArray alloc] init]; 
    pickupDateArray = [[NSMutableArray alloc] init]; 
    paidArray = [[NSMutableArray alloc] init]; 
    idArray = [[NSMutableArray alloc] init]; 

    //NSString *path = [[NSBundle mainBundle] pathForResource:@"tt" ofType:@"db"]; 


    [self getList]; 

} 

- (void)getList 
{ 
    db = [FMDatabase databaseWithPath:[Utility getDatabasePath]]; 
    [shipperCityArray removeAllObjects]; 
    [pickupDateArray removeAllObjects]; 
    [paidArray removeAllObjects]; 
    [idArray removeAllObjects]; 

    [db open]; 

    FMResultSet *fResult= [db executeQuery:@"SELECT * FROM TTLogObject"]; 


    while([fResult next]) 
    { 
     [shipperCityArray addObject:[fResult stringForColumn:@"shipperCity"]]; 
     [pickupDateArray addObject:[fResult stringForColumn:@"pickupDate"]]; 
     [paidArray addObject:[NSNumber numberWithBool:[fResult boolForColumn:@"paid"]]]; 
     [idArray addObject:[NSNumber numberWithInteger:[fResult intForColumn:@"id"]]]; 
     NSLog(@"%@", [NSNumber numberWithInteger:[fResult intForColumn:@"id"]]); 
    } 


    [db close]; 

    [self.tableView reloadData]; 
} 

问题是数据从数据库中被删除得很好。但是,按下删除之后,表视图不再显示任何单元格。当我重新启动应用程序时,正确的数据再次被加载,被删除的单元格实际上已经消失。我怀疑它与numberOfRowsInSection有关。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSLog(@"%i", [shipperCityArray count]); 
    return [shipperCityArray count]; 
} 

当应用程序启动时,它打印细胞适量,但删除时被击中,它没有显示任何信息,而且似乎不被调用。

我试图使用[self.tableView beginUpdates][self.tableView endUpdates],但这些似乎错误地指出删除后产生的错误项数。我不知道如何解决这个问题。如果我必须使用beginUpdates和endUpdates,有人可以向我解释这应该如何正确完成,并且实际上发生了什么?

回答

4

而不是调用reloadData,你需要明确告诉你的tableView你正在删除一个单元格。更换

[self.tableView reloadData]; 

[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 

你应该在你commitEditingStyle方法调用它。

+0

我正在尝试使用,我通过什么deleteRowsAtIndexPaths?现在,如果我只是离开代码行,我得到这个错误: - [NSIndexPath计数]:无法识别的选择器发送到实例0x7566e50 2013年8月19日22:57:28.808货运追踪器[13861:c07] ** *终止应用程序,由于未捕获的异常'NSInvalidArgumentException',原因:' - [NSIndexPath计数]:无法识别的选择器发送到实例0x7566e50' – user1933131

+0

它需要一个索引路径数组,而不是一个,所以你必须给它一个单元数组。我刚刚更新了它,对此感到遗憾。 –

+0

啊,我试过了。如果我这样做,它给了我这个:***由于未捕获的异常'NSInternalInconsistencyException',原因:'无效的更新:在0节中的行数无效终止应用程序。包含在现有节后的行数更新(2)必须等于更新前(2)的该部分中包含的行数,加上或减去从该部分插入或删除的行数(0插入,1删除)和正数或负数的行移入或移出该部分(移入0,移出0)。 – user1933131