【发布时间】:2012-10-15 22:59:40
【问题描述】:
以下情况:我想从我的 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:终止调用抛出异常
【问题讨论】:
-
您介意发布您的
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section方法吗? -
也发布错误消息,您可能需要更新模型以反映已删除的行
-
快速想法(不确定是否是这样):尝试将
[self.daten removeObjectAtIndex:indexPath.row];放入beginUpdates/endUpdates块中,以便numberOfRowsInSection在之前/之后返回不同的值。 -
试过了,还是一样的例外!
-
那个方法还有更多代码吗?
标签: iphone objective-c