从UITableView删除一行导致崩溃

时间:2012-10-15 14:13:41

标签: iphone objective-c

以下情况:我想从我的UITableView中删除一行,但这总是崩溃......

@property (nonatomic,retain) NSMutableArray *daten;

@synthesize daten;

然后在我的viewDidLoad中填充我的数组:

-(NSMutableArray *) daten{
if (!daten) {
    self.daten = [[NSMutableArray alloc] init];
}

@try {
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSString *dbPath = [self getDBPath];
    BOOL success = [fileMgr fileExistsAtPath:dbPath];
    if(!success)
    {
        NSLog(@"Cannot locate database file '%@'.", dbPath);
    }
    if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
    {
        NSLog(@"An error has occured: %s", sqlite3_errmsg(db));

    }


    const char *sql = "SELECT * FROM  Daten";
    if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
    {
        NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
    }else{

        while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
            Daten * infos = [[Daten alloc] init];
            infos.download = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
            infos.upload = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,2)];
            infos.ping = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,3)];
            infos.comment = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,4)];
            infos.date = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,5)];
            infos.type = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,6)];
            infos.lati = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,7)];
            infos.longi = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,8)];


            [daten addObject:infos];
        }
    }
}
@catch (NSException *exception) {
    NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
}
@finally {
    sqlite3_finalize(sqlStatement);
    sqlite3_close(db);

    return daten;
}
}

如果我想删除一行:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source
    //[self.daten removeAllObjects];
    NSString *selectedCategory = [self.daten objectAtIndex:indexPath.row];
    NSLog(@"there are %d objects in the array", [self.daten count]);
    [self.daten removeObjectAtIndex:indexPath.row]; //we assume, that mySourceArray is a NSMutableArray we use as our data source
    NSLog(@"there are %d objects in the array", [self.daten count]);

    [self.tableView beginUpdates];

    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    [self.tableView endUpdates];

....}

它崩溃了,因为我的数组计数在删除之前和之后!但我不知道为什么......

我的NumberOfRowsInSection:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

// Return the number of rows in the section.
return [self.daten count];


}

这是例外:

2012-10-15 16:45:13.778 Speedcheck[5297:907] *** Assertion failure in -[UITableView         _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-2372/UITableView.m:1070

2012-10-15 16:45:13.779 Speedcheck [5297:907] *由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效更新:第0部分中的行数无效。更新后的现有部分中包含的行(26)必须等于更新前该部分中包含的行数(26),加上或减去从该部分插入或删除的行数(插入0,删除1) )加上或减去移入或移出该部分的行数(0移入,0移出)。 * 第一次抛出调用堆栈: (0x3adcf3e7 0x35934963 0x3adcf29d 0x390c67b3 0x36569ba1 0x82311 0x366fefe1 0x3662003f 0x3661fff3 0x3661ffcd 0x3661f883 0x3662003f 0x3661fff3 0x3661ffcd 0x3661f883 0x3661fd71 0x365485c1 0x365358a9 0x365351b7 0x3ac025f7 0x3ac02227 0x3ada43e7 0x3ada438b 0x3ada320f 0x3ad1623d 0x3ad160c9 0x3ac0133b 0x36589289 0x51d33 0x34984b20)     libc ++ abi.dylib:terminate调用抛出异常

4 个答案:

答案 0 :(得分:3)

看来问题是你的-(NSMutableArray *) daten方法。

看起来你想要进行延迟初始化,但实际上,无论何时调用此方法,都会重新构建数组。所以你的删除只有在下次再次填充数组时才会有效,直到数组再次被填满(甚至看起来它应该增长和增长)。

您可能希望拥有类似

的内容
-(NSMutableArray *) daten{
   if (daten)
      return daten;

   self.daten = [[NSMutableArray alloc] init];

   // ... same setup as before

}

代替。

答案 1 :(得分:1)

我不知道它是否有帮助,因为它对我没有意义,只是在调用[self.daten removeObjectAtIndex:indexPath.row];之后放[self.tableView beginUpdates];并将结果报告给我们!

答案 2 :(得分:0)

从UITableView中删除单元格的最简单方法是从数据数组中删除对象。

请执行以下操作:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // If row is deleted, remove it from the list.
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        NSInteger rowNumber = indexPath.row;

        [self.daten removeObjectAtIndex:rowNumber];

        [tableView reloadData];
    }
}

答案 3 :(得分:0)

你几乎拥有它。在你检查编辑风格后,你真正想要做的就是把这条线放好:

if (editingStyle == UITableViewCellEditingStyleDelete) {

[self.tableView beginUpdates];

这是因为您希望模型更改发生在beginUpdates块中,否则会出现不一致。

相关问题