2013-10-27 107 views
0

我试着去阅读我的SQLite DB行,然后将数据转换为JSON,并把它变成一个NSMutableArray。然后我想循环访问数组并将数据打印到我的table view中。如何将JSON数据添加到NSMutableArray?

这是我做的,从SQLite加载数据:

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

现在,这里是我尝试将其添加到我的table view

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

错误即时得到的是:

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

任何想法我做错了吗?

+0

你为什么要准确使用JSON? – Wain

+0

我想用它来从DB中分离我的列(我有多个标签,我想用不同的变量填充)。所以我认为id使用json,然后解析json,这样我就可以将正确的var打印到正确的标签 – Alosyius

+0

远射,但是你的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模板中忽略[...]。然后,你将有

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

顺便说一句,这样做的正确方法是有你在你的数据库中获取正确的设置适当的属性自定义对象Message