从UITableView中删除UITableViewCell

3

我正在使用fmdb来管理一些数据,这些数据显示在常规的UITableView上。我尝试使用以下代码删除一个单元格:

- (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,请有人解释一下应该如何正确地执行此操作以及实际发生了什么?
1个回答

4

你需要明确告诉你的tableView你正在删除一个cell,而不是调用reloadData。替换为

[self.tableView reloadData];

使用

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

您应该在commitEditingStyle方法中调用此方法。


我正在尝试使用它,我应该传递什么给deleteRowsAtIndexPaths?现在,如果我只是保留代码行不变,我会得到这个错误:-[NSIndexPath count]:向实例0x7566e50发送未识别的选择器。2013-08-19 22:57:28.808 Trucking Tracker[13861:c07] ***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSIndexPath count]:向实例0x7566e50发送未识别的选择器”。 - Paulius Dragunas
它期望一个索引路径数组,而不是一个,所以你必须给它一个只有一个元素的数组。我刚刚更新了它,对此感到抱歉。 - Erik Godard
啊,我已经尝试过了。如果按照所示进行操作,会出现以下错误:*** 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 (2) must be equal to the number of rows contained in that section before the update (2), 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).' - Paulius Dragunas
你需要在调用getList方法之后调用它,这样count方法才能返回正确的元素数量。 - Erik Godard
哎呀,我对这个问题真是运气不好,现在又出现了这个错误:*** 在-[UITableView _endCellAnimationsWithContext:]方法中的断言失败,/SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:862 2013-08-19 23:06:22.926 Trucking Tracker[14743:c07] *** 应用程序因未捕获的异常 'NSInternalInconsistencyException' 而终止,原因是:'尝试从只包含3行的第0节删除第3行' - Paulius Dragunas
明白了,getList() 函数中仍在调用 reloadData(),只需要将其删除即可。谢谢。 - Paulius Dragunas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接