2010-12-14 59 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; 
} 

我希望這可以幫助別人。