我在从数据库和表中删除行时遇到一些问题。
问题是:我可以滑动该行,点击“删除”按钮,但没有任何反应。
可能我做错了。
有人可以给我一个提示吗? 如果你想看一下这里有所有程序:(更新)
-(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];
}
}
答案 0 :(得分:2)
你应该打电话:
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
在commitEditingStyle:
内而不是reloadData
。另外,请确保从DB&中删除实体。从numberOfRowsInSection:
返回少一个。
答案 1 :(得分:1)
我认为问题出在这一行
NSString *insertStatementsNS = [NSString stringWithFormat: @"DELETE FROM \"shoppinglist\" WHERE key=?", indexPath];
我想你想进入insertStatementsNS
这个:
NSString *insertStatementsNS = [NSString stringWithFormat: @"DELETE FROM \"shoppinglist\" WHERE key=%@", indexPath];
或
NSString *insertStatementsNS = [NSString stringWithFormat: @"DELETE FROM \"shoppinglist\" WHERE key='%@'", indexPath];