[英]Display JSON data in UITableView
我的應用程序正在加載一些以JSON表示的數據,並且一切正常,但是當我嘗試在UITableView單元格中顯示這些數據時,什么也沒發生。 我的代碼如下:
獲取數據(JSON):
-(void)fetchedData:(NSData *)responseData {
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSArray* latestLoans = [json objectForKey:@"loans"];
testeDictionary = [latestLoans objectAtIndex:0];
testeLabel.text = [NSString stringWithFormat:@"%@",[testeDictionary objectForKey:@"id"]];
testeString = [testeDictionary objectForKey:@"username"];
[miArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:testeString,@"username",nil]];
}
UITableView:
-(UITableViewCell *)tableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = (UITableViewCell *)[self.settingsTableView dequeueReusableCellWithIdentifier:@"CellD"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CellD" owner:self options:nil];
cell = (UITableViewCell *)[nib objectAtIndex:0];
}
if ([indexPath row] == 0) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CellA" owner:self options:nil];
cell = (UITableViewCell *)[nib objectAtIndex:0];
NSDictionary *itemAtIndex = (NSDictionary *)[miArray objectAtIndex:indexPath.row];
UILabel *usernameString = (UILabel *)[cell viewWithTag:1];
usernameString.text = [itemAtIndex objectForKey:@"id"]; <== MUST DISPLAY JSON VALUE
}
return cell;
}
更清楚地,我需要在usernameString.text
上顯示此[testeDictionary objectForKey:@"id"]
?
您沒有存儲ID
[miArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:testeString,@"username",nil]];
我想你想做什么是這樣的
NSString *idString = [NSString stringWithFormat:@"%@", [testeDictionary objectForKey:@"id"]];
[miArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:
testeString, @"username",
idString, @"id",
nil]];
編輯(說明)
在您的方法fetchedData:
提取ID並將某些標簽的文本設置為ID。
testeLabel.text = [NSString stringWithFormat:@"%@",[testeDictionary objectForKey:@"id"]];
在那之后,您忘記了ID。 然后,您繼續提取用戶名,並創建僅包含用戶名的字典,並將該字典添加到名為miArray
的數組中。
[miArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:testeString,@"username",nil]];
請注意,您沒有指定任何名為“ id”的鍵。
稍后,您從miArray
獲取字典。 該詞典是您僅用一個鍵即“用戶名”創建的詞典。 您告訴它獲取鍵“ id”的對象,但是由於您從未指定該鍵,因此獲得了nil
值。
最重要的是,嘗試我的解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.