簡體   English   中英

將新數據重新加載到tableView中

[英]Reload new data into tableView

我正在使用XML解析器,SQLitedatabase和View(帶有導航欄和tableView)構建應用程序。 SQLite數據庫包含一些數據,而我想做的是,當我按下導航欄中的添加按鈕時,將解析xml內容,將其添加到SQLite數據庫中,並將數據添加到tableView中。 XML已成功解析並添加到SQLite數據庫中。 但是當我這樣做時:

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

}

在Book類中,我有一個名為getInitialDataToDisplay的void。 我想這是我必須進行一些更改,還是再次使此方法被調用的地方? 我認為問題在於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.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM