【问题标题】:imageView in TableView from json array来自json数组的TableView中的imageView
【发布时间】:2017-10-26 02:59:05
【问题描述】:

我正在使用 Objective-c 开发一个简单的 IOS 应用程序,我想将图像加载到表格单元格中。 我有一个小网络服务,它给我一个像这样的 json 数据:

[{"id":1,"name":"Papas","description":"Papas chilotas negras","images":
[{"url":"/uploads/product/images/1/8282486574_382f59c31e_o.jpg",  
"thumb":"url":"/uploads/product/images/1/
thumb_8282486574_382f59c31e_o.jpg"},
"catalog":"url":"/uploads/product/images/1/
catalog_8282486574_382f59c31e_o.jpg"},
"big":"url":"/uploads/product/images/1/
big_8282486574_382f59c31e_o.jpg"}}],
"price":1200,"stand_id":3,"created_at":"2017-10-
24T04:01:09.000Z","updated_at":"2017-10-24T04:01:09.000Z"}]

我已经设置了这样的名称和描述tableCell

问题是我如何访问拇指版本图像的路径以将其显示在表格单元格中。

这是我必须填充表格的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

NSDictionary *dictionary = [self.json objectAtIndex:indexPath.row];
cell.textLabel.text=[dictionary objectForKey:@"name"];
cell.detailTextLabel.text = [dictionary objectForKey:@"description"];
return cell;
}

【问题讨论】:

  • 这是一个有效的 JSON 吗?
  • URL 无效 -> "thumb" : "url" : " /uploads/product/... /_o.jpg"
  • json 数据来自这个 url 18.221.78.126/ecoferia/api/products 它来自一个带有 mysql db 的 Rails 应用程序。

标签: ios objective-c json tableview


【解决方案1】:

您提供的 JSON 数据有误。正确的类型是

("thumb" : "/uploads/product/images/1/thumb_8282486574_382f59c31e_o.jpg" ),

没有“网址”。

因此,您可以获得拇指图像字典:

NSArray * arr = dictionary[@"images"];

比如

[arr[0] objectForKey:@"thumb"];

【讨论】:

    【解决方案2】:

    this 链接中的 JSON 与您在问题中提供的 JSON 不同。

    根据链接中的 JSON,您可以这样解析它:

    [[[[dictionary objectForKey:@"images"] objectAtIndex:0] valueForKey:@"thumb"] valueForKey:@"url"];
    

    【讨论】:

    • @AndresAldana 太好了。请接受并投票赞成答案。
    猜你喜欢
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    • 2022-01-08
    • 2015-07-20
    • 1970-01-01
    相关资源
    最近更新 更多