2010-12-14 57 views
0

我已到处寻找这个,但我无法找到确切的答案......都有轻微的变化。嵌套到UITableView的NSArray

无论如何,我叫一个JSON页面返回以下(从的NSLog):

{ 
    messages =   { 
     1 =    { 
      Body = "This is the body of message 1"; 
      Title = "Message 1"; 
     }; 
     2 =    { 
      Body = "This is the body of message 2"; 
      Title = "Message 2"; 
     }; 
    }; 
} 

我然后将数据保存到一个NSDictionary中(称为messageArray)。 (数组被一个NSMutableArray的)

然后我做的:

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    //put rowsarray into dictionary 
    NSDictionary *dictionary = [messageArray objectAtIndex:indexPath.section]; 

    //new dictionary into array 
    NSArray *messages = [dictionary objectForKey:@"messages"]; 

    NSLog(@"the message array = %@",messages); 

    //this fails 
    cell.textLabel.text = [messages objectAtIndex:indexPath.row]; 

return cell; 

返回的NSLog(所以我假设我的JSON阵列工作正常):

the message array = { 
1 =  { 
    Body = "This is the body of message 1"; 
    Title = "Message 1"; 
}; 
2 =  { 
    Body = "This is the body of message 2"; 
    Title = "Message 2"; 
}; 

}

我明白,我没有正确标记textlabels.text,但我不知道如何去循环“消息”数组,显示数组中的所有“标题”值,以显示在我的用户界面上TableView列表。

我确定我错过了这么简单的事情......但它直到现在都没有了。任何链接欢迎...我将继续寻找自己....

回答

1
NSArray *messages = [dictionary objectForKey:@"messages"]; 

这是什么线的需要,如果你得到你的字典这里

NSDictionary *dictionary = [messageArray objectAtIndex:indexPath.section]; 

现在,你知道什么是用于连接数据字典的密钥。

然后简单地

cell.textLabel.text = [dictionary valueForKey:@"Title"]; 
cell.detailTextLabel.text= [dictionary valueForKey:@"Body"]; 
0

此调用

看起来是返回一个字典,你试图把它塞进一个测试场。我会确认这是第一个字典,类似于:

NSLog(@"%@", [messages objectAtIndex:indexPath.row]) 

如果这不是一个字符串,那可能是您的问题。

为了得到你想要的字符串,你可以这样做:

[[messages objectAtIndex:indexPath.row] valueForKey:@"Title"] 
+0

我已经加入此行:的NSLog(@ “%@”,[消息objectAtIndex:indexPath.row]),和没有爱。我得到无法识别的选择器发送到实例。 – btwong 2010-12-14 03:14:33

+0

尝试记录[消息类] – MCannon 2010-12-14 22:02:30

0
cell.textLabel.text = [[messages objectAtIndex:indexPath.row] objectForKey:@"Title"]; 
cell.detailTextLabel.detailLabel = [[messages objectAtIndex:indexPath.row] objectForKey:@"Body"]; 
+0

我已经尝试设置textLabel的想法,我什么都没有。我想我必须看我的字典和/或得到一个字符串。 – btwong 2010-12-14 03:17:43

+0

你用什么将json对象转换为字典? – griotspeak 2010-12-14 04:08:04

0

我有“半成品”排序的问题。我已删除从JSON数据索引所以现在它看起来像:

messages =  (
      { 
     Body = "This is the body of message 1"; 
     Title = "Message 1"; 
    }, 
      { 
     Body = "This is the body of message 2"; 
     Title = "Message 2"; 
    } 
); 

,并使用了该对表的填充:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    //put rowsarray into dictionary 
    NSDictionary *dictionary = [messageArray objectAtIndex:indexPath.section]; 

    NSLog(@"dictionary = %@", dictionary); 

    //new dictionary into array 
    NSArray *messages = [dictionary objectForKey:@"messages"]; 

    //NSArray *message=[[messages allKeys] sortedArrayUsingSelector:@selector(compare:)]; 
    NSLog(@"array messages = %@", messages); 

    cell.textLabel.text = [[messages objectAtIndex:indexPath.row] objectForKey:@"Title"]; 

    return cell; 
} 

我希望这可以帮助别人。