我正在尝试实现一种功能,当单击uitabbar上的刷新按钮时,会调用一个返回某些数据的Web服务。数据存储在sqlite db中,然后在tableview上呈现。我的问题是,当从服务中获取新数据时,db会更新,但更新的记录不会立即显示在tableview中。我必须重新启动应用程序才能看到更新的记录。下面是我点击重新加载按钮时调用的代码:
更新代码:
-(void)get_vrns {
NSLog(@"Refreshing...");
NSString *delete = [NSString stringWithFormat:@"DELETE FROM VRN"];
NSString *insert = [NSString stringWithFormat:@"INSERT INTO VRN ('VRNumber') VALUES ('%@')", @"LEC8-9436"];
insert = @"INSERT INTO VRN ('VRNumber') VALUES ('LEC-876'); INSERT INTO VRN ('VRNumber') VALUES ('AAA-564');INSERT INTO VRN ('VRNumber') VALUES ('ABD-908'); INSERT INTO VRN ('VRNumber') VALUES ('AHF-540');";
char *err;
if (sqlite3_exec(db, [delete UTF8String], NULL, NULL, &err) == SQLITE_OK) {
if (sqlite3_exec(db, [insert UTF8String], NULL, NULL, &err) != SQLITE_OK) {
sqlite3_close(db);
NSAssert(0, @"Could not update table");
}
else {
NSLog(@"Table updated");
}
}
else {
sqlite3_close(db);
NSAssert(0, @"Could not delete records");
}
[self.vrnTable reloadData];
NSString *select = [NSString stringWithFormat:@"SELECT VRNumber FROM VRN"];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(db, [select UTF8String], -1, &statement, nil) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
char *vrn = (char *) sqlite3_column_text(statement, 0);
NSString *vrnStr = [[NSString alloc] initWithUTF8String:vrn];
NSString *str = [[NSString alloc] initWithFormat:@"%@", vrnStr];
[_entries addObject:str];
}
}
else {
NSAssert1(0, @"Error: Because %s", sqlite3_errmsg(db));
}
NSLog(@"%@", self.entries);
}
答案 0 :(得分:0)
想知道在哪里存储数据以便在tableview中显示。我的意思是我们通常使用NSArray来存储tableview的数据,在tableView中使用它:cellForRowAtIndexpath:方法我们从数组中获取数据并将其放在单元格上。您是从sqlite表还是正在使用的任何变量更新该数组。
更新代码:
-(void)get_vrns {
NSLog(@"Refreshing...");
NSString *delete = [NSString stringWithFormat:@"DELETE FROM VRN"];
NSString *insert = [NSString stringWithFormat:@"INSERT INTO VRN ('VRNumber') VALUES ('%@')", @"LEC8-9436"];
insert = @"INSERT INTO VRN ('VRNumber') VALUES ('LEC-876'); INSERT INTO VRN ('VRNumber') VALUES ('AAA-564');INSERT INTO VRN ('VRNumber') VALUES ('ABD-908'); INSERT INTO VRN ('VRNumber') VALUES ('AHF-540');";
char *err;
if (sqlite3_exec(db, [delete UTF8String], NULL, NULL, &err) == SQLITE_OK) {
if (sqlite3_exec(db, [insert UTF8String], NULL, NULL, &err) != SQLITE_OK) {
sqlite3_close(db);
NSAssert(0, @"Could not update table");
}
else {
NSLog(@"Table updated");
}
}
else {
sqlite3_close(db);
NSAssert(0, @"Could not delete records");
}
[self.entries removeAllObjects];
NSString *select = [NSString stringWithFormat:@"SELECT VRNumber FROM VRN"];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(db, [select UTF8String], -1, &statement, nil) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
char *vrn = (char *) sqlite3_column_text(statement, 0);
NSString *vrnStr = [[NSString alloc] initWithUTF8String:vrn];
NSString *str = [[NSString alloc] initWithFormat:@"%@", vrnStr];
[_entries addObject:str];
}
}
else {
NSAssert1(0, @"Error: Because %s", sqlite3_errmsg(db));
}
NSLog(@"%@", self.entries);
[self.vrnTable reloadData];
}