简体   繁体   English

从UITableView删除UITableViewCell

[英]Deleting a UITableViewCell from UITableView

I am using fmdb to manage some data that is display on a regular UITableView. 我正在使用fmdb来管理在常规UITableView上显示的一些数据。 I am attempting to delete one cell using this code: 我试图使用此代码删除一个单元格:

- (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];
    }
}

Here is the code for viewDidLoad and the getList function that I am using. 这是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];
}

The issue is that the data gets deleted just fine from the database. 问题是数据从数据库中删除得很好。 However, after delete is pressed, the table view does not display any cells anymore. 但是,按下删除后,表视图不再显示任何单元格。 When I restart the app, the proper data gets loaded again, with the cell that was deleted actually gone. 当我重新启动应用程序时,再次加载正确的数据,删除的单元格实际上已经消失。 I suspect it has something to do with numberOfRowsInSection . 我怀疑它与numberOfRowsInSection

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

When the app is launched, it prints the proper amount of cells, but when delete is hit, it does not print anything, and seems to not be called. 当应用程序启动时,它会打印适当数量的单元格,但是当点击删除时,它不会打印任何内容,并且似乎不会被调用。

I have attempted to use [self.tableView beginUpdates] and [self.tableView endUpdates] but those seem to error out stating something about the wrong number of items resulting after the delete. 我试图使用[self.tableView beginUpdates][self.tableView endUpdates]但这些似乎错误地说明删除后产生的错误数量的项目。 I am not sure how to solve this. 我不知道如何解决这个问题。 If I must use beginUpdates and endUpdates, can someone explain to me how this should be done properly, and what is actually going on? 如果我必须使用beginUpdates和endUpdates,有人可以向我解释这应该如何正确完成,以及实际发生了什么?

Instead of calling reloadData, you need to explicitly tell your tableView you are deleting a cell. 您需要明确告诉tableView删除单元格,而不是调用reloadData。 Replace 更换

[self.tableView reloadData];

with

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

You should call this in your commitEditingStyle method. 你应该在你的commitEditingStyle方法中调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM