【问题标题】:Reload new data into tableView将新数据重新加载到 tableView
【发布时间】:2011-06-11 22:43:52
【问题描述】:

我正在构建一个带有 XML 解析器、SQLite 数据库和视图(带有导航栏和 tableView)的应用程序。 SQLite 数据库包含一些数据,我想要做的是,当我按下导航栏中的添加按钮时,会解析 xml 内容,将其添加到 SQLite 数据库中,并将数据添加到 tableView 中。 XML 成功地被解析并添加到 SQLite 数据库中。但是当我这样做时:

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

只加载新数据,现有数据消失。当我终止应用程序并重新启动它时,数据会正确加载,并且我会在 tableView 中获取所有数据。

所以我的问题是:如何将新数据添加到 tableView 并仍然查看现有数据?

【问题讨论】:

  • 你能粘贴你的代码吗?看起来问题不是重新加载 tableView 数据,而是读取内容。你确定你正在从 SQLite 数据库中读取内容吗?你确定你是在解析 XML 之后做的吗?

标签: iphone sqlite ios uitableview


【解决方案1】:

我的 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.
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多