2011-01-19 16 views
0

XML 파서, SQL 데이터베이스 및보기 (탐색 모음 및 tableView 포함)를 사용하여 응용 프로그램을 작성하고 있습니다. SQLite 데이터베이스에는 일부 데이터가 포함되어 있으며 탐색 바의 추가 버튼을 누르면 XML 컨텐트가 파싱되어 SQLite 데이터베이스에 추가되고 데이터가 tableView에 추가됩니다. XML은 성공적으로 파싱되어 SQLite 데이터베이스에 추가됩니다. 하지만 이렇게하면 :새 데이터를 tableView에 다시로드하십시오.

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

새로운 데이터 만로드되고 기존 데이터는 사라집니다. 응용 프로그램을 종료하고 다시 시작하면 데이터가 올바르게로드되고 tableView 내부의 모든 데이터를 가져옵니다.

내 질문은 : 어떻게 새 데이터를 tableView에 추가하고 여전히 기존 데이터를 볼 수 있습니까?

+0

코드를 붙여 넣을 수 있습니까? 문제는 tableView 데이터를 다시로드하는 것이 아니라 내용을 읽는 것입니다. SQLite 데이터베이스에서 내용을 읽으 실 건가요? 그리고 XML을 파싱 한 후이 작업을 수행하고 있습니까? – Trinca

답변

0

내 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. 
} 
관련 문제