2013-05-01 60 views
0

我需要在UITableView中显示"href"网址为cell.textlabel.text。任何帮助将不胜感激。从JSON响应中显示cell.textLabel.text

JSON:

"grandfathers": [{ 
     "id": "60", 
     "sons": [{ 
      "id": "30", 
      "grandsons": [{ 
       "id": "10", 
       "links": { 
        "api": { 
         "news": { 
          "href": "http://api.website.com/v1/family/grandfathers/sons/grandsons/10/news" 
         }, 
        }, 

@interface:

@property (strong, nonatomic) NSArray *grandfathers; 
@property (strong, nonatomic) NSMutableArray *sons; 
@property (strong, nonatomic) NSArray *grandsons; 

表视图:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"standardCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    Grandfather *grandfatherLocal = [grandfathers objectAtIndex:0]; 
    Son *sonLocal = [grandfatherLocal.sons objectAtIndex:0]; 

    // Not sure what to do here to get to the "href" eventually 
    // This was my best guess... 
    Grandson *grandsonLocal = [sonLocal.grandsons objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [grandsonLocal.links.api.news.href valueForKey:@"href"]; 
}; 

我不知道怎么弄到当时的嵌套数据。 (FYI我建立模型和现在用RestKit,所以多数民众赞成我在这里做什么的情况下。)

编辑: 我收到的表格显示任何内容,并且在输出误差只是: “找不到的keyPath对象映射: ''”

编辑: 孙子每个请求接口:

@interface Team : NSObject 
@property (nonatomic, strong) NSNumber *id; 
@property (nonatomic, strong) Links *links; 
+ (RKObjectMapping *) mapping; 
@end 

编辑: Alessandro完美地回答了我的问题,请参阅下文。顺便说一句,他超越和帮助我,我超级伟大 -

谢谢!

+0

可能相关 - “细胞”可能是零,具体取决于您的设置。你有没有为你的单元注册class/nib文件?从文档:_“如果没有单元可用于重用并且未注册类或nib文件,则此方法返回nil。”_ – 2013-05-01 20:32:03

+0

valueForKeyPath:@“links.api.news.href”? – Marc 2013-05-01 20:40:46

+0

@AaronBrager细胞不是零,但谢谢你的头抬起来,以防万一它是 – Realinstomp 2013-05-01 20:41:56

回答

1

如果grandsonLocal能够正确读取,那么我认为你应该只使用

cell.textLabel.text = grandsonLocal.links.api.news.href; 

如果这不起作用,尝试把一个断点在该行和检查的grandsonLocal及其属性的内容。

+0

'grandsonLocal'正在提取正确,因为在前面的'UIViewController'我列出了与'UITableView'相关的所有'id' ...所以出于某种原因我不确定如何从'links'正确映射一路'href' – Realinstomp 2013-05-01 21:04:05

+0

你可以发布Grandson @interface吗?它应该有一个链接的NSDictionary属性,或者一个链接属性,指向类似于“链接”对象 – 2013-05-01 21:06:48

+0

的东西,我只是发布了代码。你是对的,有一个'Links'属性指向'Links'对象(它有一个属性'api',指向'api'对象,它有一个属性'news'指向'news'对象,它具有'NSString'' href'属性并映射'href') – Realinstomp 2013-05-01 21:18:47