简体   繁体   English

将新数据重新加载到tableView中

[英]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). 我正在使用XML解析器,SQLitedatabase和View(带有导航栏和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. SQLite数据库包含一些数据,而我想做的是,当我按下导航栏中的添加按钮时,将解析xml内容,将其添加到SQLite数据库中,并将数据添加到tableView中。 The XML successfully gets parsed and added in the SQLite database. XML已成功解析并添加到SQLite数据库中。 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. 当我终止应用程序并重新启动它时,数据已正确加载,并且所有数据都在tableView内部。

So my question is: How do I add the new data into the tableView and still view the existing data? 所以我的问题是:如何将新数据添加到tableView中并仍然查看现有数据?

My add_clicked void is: 我的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];

}

Inside my Book class i have a void called getInitialDataToDisplay. 在Book类中,我有一个名为getInitialDataToDisplay的void。 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. 我认为问题在于tableView仅在应用程序加载时读取sqlite内容。 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.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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