将新数据重新载入tableView
问题描述:
我正在用XML解析器,SQLitedatabase和View(带有导航栏和tableView)构建应用程序。 SQLite数据库包含一些数据,我试图做的是,当我在导航栏中添加按钮时,xml内容被解析,添加到SQLite数据库中,并且数据被添加到tableView中。 XML成功解析并添加到SQLite数据库中。但当我这样做:将新数据重新载入tableView
-(void)add_Clicked:(id)sender {
......
[self.tableView reloadData]
}
只有新的数据加载和现有的数据已经消失。当我终止应用程序并重新启动它时,数据被正确加载,并且我得到了tableView中的所有数据。
所以我的问题是:如何将新数据添加到tableView中,并仍然查看现有数据?
答
我add_clicked无效是:
- (void) add_Clicked:(id)sender {
NSURL *url = [[NSURL alloc] initWithString:@"http://sites.google.com/site/iphonesdktutorials/xml/Books.xml"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];
//Set delegate
[xmlParser setDelegate:parser];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
if(success)
NSLog(@"No Errors");
else
NSLog(@"Error Error Error!!!");
[self.tableView reloadData];
}
在我的书类,我有一个叫做getInitialDataToDisplay无效。我认为这是我必须做一些改变,或让这个方法再次调用?我认为问题是,tableView只读取应用程序加载时的sqlite内容。是的,我完全没有编程经验,但我已经学会了一点。
+ (void) getInitialDataToDisplay:(NSString *)dbPath {
BooksAppDelegate *appDelegate = (BooksAppDelegate *)[[UIApplication sharedApplication] delegate];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
const char *sql = "select bookID, title from books";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
Book *bookObj = [[Book alloc] initWithPrimaryKey:primaryKey];
bookObj.title = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
bookObj.isDirty = NO;
[appDelegate.bookArray addObject:bookObj];
[bookObj release];
}
}
}
else
sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}
你能粘贴你的代码吗?看起来问题不是重新加载tableView数据,而是读取内容。你确定你正在阅读SQLite数据库的内容吗?你确定你在解析XML之后做了这件事吗? – Trinca 2011-01-19 18:40:52