简体   繁体   中英

Reload new data into tableView

I'm building an app with a XML parser, a SQLitedatabase and a View (with a navigationbar and a tableView). The SQLite database contains some data and what I'm trying to do is that when I push the add button in the navigationbar the xml content gets parsed, added into the SQLite database and the data is added in the tableView. The XML successfully gets parsed and added in the SQLite database. But when I do this:

-(void)add_Clicked:(id)sender {
    ......
    [self.tableView reloadData]
}

Only the new data is loaded and the existing data is gone. When I terminate the application and restart it the data is loaded correctly and I get all the data inside the tableView.

So my question is: How do I add the new data into the tableView and still view the existing data?

My add_clicked void is:

- (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];

}

Inside my Book class i have a void called getInitialDataToDisplay. I presume this is where I have to do some changes, or get this method calles again? I think the problem is that the tableView only reads the sqlite content when the application loads. And yes, I have absolutely no experience in programming, but I have learned a little bit.

+ (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.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM