我在从数据库和表中删除行时遇到了一些问题。
问题是:我可以滑动行,点击“删除”按钮,但没有任何反应。
可能我做错了。
有人可以给我提示吗? 如果您想看一下,这里有所有程序:(更新)
-(void)tableView:(UITableView *)_tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSDictionary *rowVals = (NSDictionary *) [shoppingListItems objectAtIndex:indexPath.row];
NSString *keyValue = (NSString *) [rowVals objectForKey:@"key"];
[_tableView beginUpdates];
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
//Here i got the SIGABRT error
[_tableView endUpdates];
sqlite3 *db;
int dbrc; //Codice di ritorno del database (database return code)
DatabaseShoppingListAppDelegate *appDelegate = (DatabaseShoppingListAppDelegate*) [UIApplication sharedApplication].delegate;
const char *dbFilePathUTF8 = [appDelegate.dbFilePath UTF8String];
dbrc = sqlite3_open(dbFilePathUTF8, &db);
if (dbrc) {
NSLog(@"Impossibile aprire il Database!");
return;
}
sqlite3_stmt *dbps; //Istruzione di preparazione del database
NSString *deleteStatementsNS = [NSString stringWithFormat: @"DELETE FROM \"shoppinglist\" WHERE key='%@'", keyValue];
const char *deleteStatement = [deleteStatementsNS UTF8String];
dbrc = sqlite3_prepare_v2(db, deleteStatement, -1, &dbps, NULL);
dbrc = sqlite3_step(dbps);
//faccio pulizia rilasciando i database
sqlite3_finalize(dbps);
sqlite3_close(db);
}
}
#
感谢 Akshay,我终于修复了这部分代码。我在这里为需要它的人写下解决方案。
-(void)tableView:(UITableView *)_tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSDictionary *rowVals = (NSDictionary *) [shoppingListItems objectAtIndex:indexPath.row];
NSString *keyValue = (NSString *) [rowVals objectForKey:@"key"];
[tableView beginUpdates];
sqlite3 *db;
int dbrc; //Codice di ritorno del database (database return code)
DatabaseShoppingListAppDelegate *appDelegate = (DatabaseShoppingListAppDelegate*) [UIApplication sharedApplication].delegate;
const char *dbFilePathUTF8 = [appDelegate.dbFilePath UTF8String];
dbrc = sqlite3_open(dbFilePathUTF8, &db);
if (dbrc) {
NSLog(@"Impossibile aprire il Database!");
return;
}
sqlite3_stmt *dbps; //Istruzione di preparazione del database
NSString *deleteStatementsNS = [NSString stringWithFormat: @"DELETE FROM \"shoppinglist\" WHERE key='%@'", keyValue];
const char *deleteStatement = [deleteStatementsNS UTF8String];
dbrc = sqlite3_prepare_v2(db, deleteStatement, -1, &dbps, NULL);
dbrc = sqlite3_step(dbps);
//faccio pulizia rilasciando i database
sqlite3_finalize(dbps);
sqlite3_close(db);
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[shoppingListItems removeObjectAtIndex:indexPath.row];
[tableView endUpdates];
[tableView reloadData];
}
}
最佳答案
你应该调用:
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
在 commitEditingStyle:
中,而不是 reloadData
。此外,确保从数据库中删除实体并从 numberOfRowsInSection:
中少返回一个实体。
关于iphone - 从表和 sqlite 数据库中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7298502/