2013-10-27 3 views
0

SQLite DB에서 행을 읽은 다음 데이터를 JSON으로 변환하고 NSMutableArray에 넣으려고합니다. 그런 다음 배열을 반복하고 데이터를 내 table view에 인쇄하고 싶습니다.NSMutableArray에 JSON 데이터를 추가하는 방법은 무엇입니까?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"ConvCell"; 
    ConvCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
    if (cell == nil) { 
     cell = [[ConvCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    NSArray *tableData = [self.entries valueForKey:@"message"]; 
    NSLog(@"Message=%@", tableData); 

    cell.visitorName.text = (NSString *) [self.entries objectAtIndex:indexPath.row]; // This trows an error 
    cell.visitorAvatar.image = [UIImage imageNamed:@"hello.png"]; 
    cell.messageTime.text = @"19.10"; 


    return cell; 
} 

오류 메신저가 점점 : 지금 여기에 내가 나의 table view에 추가하려고하는 방법이다

entries = [[NSMutableArray alloc] init]; 
NSString *sql = [NSString stringWithFormat:@"SELECT * FROM chatHistory GROUP BY channelID ORDER BY time DESC"]; 
sqlite3_stmt *statement; 

if(sqlite3_prepare_v2([box db], [sql UTF8String], -1, &statement, nil) == SQLITE_OK) { 
    while(sqlite3_step(statement) == SQLITE_ROW) { 
     char *field1 = (char *) sqlite3_column_text(statement, 0); 
     NSString *channelID = [[NSString alloc] initWithUTF8String:field1]; 

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

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

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

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

     NSString *messageArray = [[NSString alloc] initWithFormat:@"[{ \"channelID\":\"%@\", \"sender\":\"%@\", \"message\":\"%@\", \"recipient\":\"%@\", \"time\":\"%@\"}]", channelID, sender, message, recipient, time]; 

     // Convert to JSON object: 
     NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:[messageArray dataUsingEncoding:NSUTF8StringEncoding] 
                   options:0 error:NULL]; 

     [entries addObject:jsonObject]; 

을 :

내가 SQLite에서 데이터를로드 할 것입니다

exception 'NSInvalidArgumentException', reason: '-[__NSCFArray length]: unrecognized selector sent to instance 0xa0cfc40' 

내가 잘못하고있는 아이디어가 있습니까?

+0

왜 JSON을 정확히 사용 하시겠습니까? – Wain

+0

DB에서 필자의 칼럼을 분리하기 위해 그것을 사용하고 싶다. (필자는 다른 변수로 채우기를 원한다.) 그래서 id는 json을 사용하고 json을 파싱하여 올바른 lable을 인쇄 할 수 있다고 생각했습니다. – Alosyius

+0

long shot, numberOfRowsInSection은 어떻게 생겼습니까? –

답변

0

JSON을 사용하면 재미있는 일입니다. 사전을 만들어 배열에 중첩하는 간단한 코드 방법입니다. 실제로 필요한 것보다 많은 컨테이너가 있으며 JSON 논쟁 없이도 사전을 만들 수 있습니다. 당신이 그 중첩 된 배열이 바로 그 아래로 파고되지 않은 있습니다 때문에

cell.visitorName.text = (NSString *) [self.entries objectAtIndex:indexPath.row]; 

cell.visitorName.text = (NSString *) [tableData objectAtIndex:indexPath.row]; 

을해야한다 :이 라인 때문에 말했다

는 실제 오류입니다. ..

0

설정이 이상적이지 않습니다. 그 말은 ...

당신은 문자열로 메시지 배열을 캐스팅하고 있습니다.

[self.entries objectAtIndex:indexPath.row] 

은 문자열이 아닌 배열을 반환해야합니다. 당신이 원하는 것은이 같은 것입니다 :

NSArray* jsonArray = [self.entries objectAtIndex:indexPath.row]; 
NSDictionary *dictionary = [jsonArray lastObject]; 
cell.visitorName.text = dictionary[@"sender"]; 

당신은 배열로 한 사전을 포장하는 것은 정말 이해가되지 않는 것을 볼 수 있습니다. 대신 JSON 템플릿에 [...]을 남겨 둘 수 있습니다. 그런 다음 BTW

NSDictionary *dictionary = [self.entries objectAtIndex:indexPath.row]; 

있을 것입니다,이 작업을 수행하는 적절한 방법은 데이터베이스 가져 오기 바로 설정 적합한 특성을 가진 사용자 지정 개체 Message하는 것입니다.

관련 문제