以下情况:我想从我的 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:终止叫做抛出一个异常