2013-05-01 57 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