2012-09-06 5 views
0

누구든지 나를 도울 수 있습니까? 나는 지난 2-3 일 동안이 NSMutableArray에 붙어 있습니다. 나는 Objective-C 초보자이며 코드에서 오류가 무엇인지 모르겠다. 내 프로그램에서, 나는 sqlite 데이터베이스를 만들었다. 이것은 3 개의 필드를 포함하고 있으며 성공적으로 db.And 값을 삽입했다. db에서 NSMutableArray에이 값을 표시하고이 배열을 표시합니다. 그러나 문제는 마지막 요소 만 표시되며 전체 데이터는 표시되지 않습니다. 나는 왜 그것이 단지 마지막 요소를 표시하는지 이해하지 못한다. 나는 아래 코드를 붙여 넣을 것이다. 누구든지 그것을 확인하고 도와 주실 수 있습니까?NSMutableArray는 배열의 전체 데이터가 아닌 마지막 요소 만 인쇄합니다.

- (void)viewDidLoad 
{ 

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [documentPaths objectAtIndex:0]; 

    databasePath = [documentsDir stringByAppendingPathComponent:@"TDL1.db"]; 

    const char *dbpath = [databasePath UTF8String]; 

    sqlite3_stmt *statement; 

     if (sqlite3_open(dbpath, &TDLDB) == SQLITE_OK) 
     { 
        NSString *querySQL = [NSString stringWithFormat: @"SELECT MYTASK, SUBTASK FROM TDL1 "]; 

        const char *query_stmt = [querySQL UTF8String]; 



     if (sqlite3_prepare_v2(TDLDB, query_stmt, -1, &statement, NULL) == SQLITE_OK) 

     { 
      while (sqlite3_step(statement) == SQLITE_ROW) 
       { 
        todolist2=[[NSMutableArray alloc]init]; 

         NSString *mytask = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)]; 

         NSString *subtask = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)]; 

        todolist2 = [NSMutableArray arrayWithObjects:mytask,subtask, nil]; 

         NSLog(@"THE array is:%@",todolist2); 

     } 

       sqlite3_finalize(statement); 
} 
       sqlite3_close(TDLDB); 

} 
     NSLog(@"THE outside array is:%@",todolist2); 
[super viewDidLoad]; 

}

그리고 콘솔에서 나는 다음과 같은 출력을 얻고있다

: -

2012-09-06 11:33:23.529 TDL[823:f803] THE array is:(
    task0, 
    task0 
) 
2012-09-06 11:33:23.532 TDL[823:f803] THE array is:(
    task1, 
    task1 
) 
2012-09-06 11:33:23.533 TDL[823:f803] THE array is:(
    task2, 
    task2 
) 
2012-09-06 11:33:23.534 TDL[823:f803] THE array is:(
    task3, 
    task3 
) 
2012-09-06 11:33:23.534 TDL[823:f803] THE array is:(
    task4, 
    task4 
) 
2012-09-06 11:33:23.535 TDL[823:f803] THE array is:(
    task5, 
    task5 
) 
2012-09-06 11:33:23.536 TDL[823:f803] THE array is:(
    task6, 
    task6 
) 
2012-09-06 11:33:23.537 TDL[823:f803] THE array is:(
    task6, 
    task6 
) 
2012-09-06 11:33:23.537 TDL[823:f803] THE array is:(
    task7, 
    task7 
) 
2012-09-06 11:33:23.538 TDL[823:f803] THE array is:(
    task7, 
    task7 
) 
2012-09-06 11:33:23.539 TDL[823:f803] THE array is:(
    task8, 
    task8 
) 
2012-09-06 11:33:23.539 TDL[823:f803] THE array is:(
    task9, 
    task9 
) 
2012-09-06 11:33:23.540 TDL[823:f803] THE array is:(
    task10, 
    task10 
) 
2012-09-06 11:33:23.540 TDL[823:f803] THE outside array is:(
    task10, 
    task10 
) 
2012-09-06 11:33:23.541 TDL[823:f803] could not prepare statement: not an error 

나는 그것을 보여 task10.How에 task0부터 값을 표시 할 가르쳐주세요 나 해결책. 감사합니다.

+0

그 배열을 안으로 만들었습니다. while while while while 루프 –

+0

@problemchild : 고마워요. 알아 들었어. :) – Div

+0

@problemchild : 나는이 모든 항목을 UItableView에 표시하고 있습니다. 이제 section.Means의 첫 번째 요소와 섹션의 두 번째 요소에 첫 번째 배열 요소를 표시하려고합니다. 어떻게하는지 말해 줄 수 있니? – Div

답변

1

while (sqlite3_step(statement) == SQLITE_ROW) 루프를 통과 할 때마다 새로운 NSMutableArray를 만들고있는 중입니다. 실제로 당신은 하나를 두 번 만들고 있습니다. 사용하는 대신

todolist2 = [NSMutableArray arrayWithObjects:mytask,subtask, nil]; 

:

[todolist2 addObject:myTask]; 
[todolist addObject:subTask]; 

이 줄을 당신의 while 루프 내부에 그런

todolist2=[[NSMutableArray alloc]init]; 

를 대신하여 새로운 배열 매번 다시 : while 루프 외부에서 다음 줄을 이동 배열의 끝에 두 문자열을 추가합니다.

+0

고맙습니다. 알아. :) 나는 의심이 하나 더 있습니다. UItableView에 이러한 모든 항목을 표시하고 있습니다. 이제 section.Means의 첫 번째 배열 요소와 섹션의 두 번째 요소에 이러한 항목을 표시하려고합니다. 어떻게하는지 말해 줄 수 있니? – Div

관련 문제