【问题标题】:Parse 2 images from JSON to UITableViewCell将 2 个图像从 JSON 解析为 UITableViewCell
【发布时间】:2014-08-20 20:12:20
【问题描述】:

我能够将单个图像放入 UITableViewCell。这是我需要的任务是将两个两个图像放入 UITableViewCell。我在下面粘贴的代码是我用来解析一张图像的。建议我更改获取 2 张图片。

 jsonDict = [jsonArr objectAtIndex:indexPath.row];
NSLog(@"imagess%@",jsonDict);
cell.textLabel.text = [jsonDict objectForKey:@"ImageName"];
cell.textLabel.textAlignment=NSTextAlignmentRight;

NSData *imgData = [NSData dataWithBase64EncodedString:[jsonDict objectForKey:@"ImageData"]];
cell.imageView.image = [UIImage imageWithData:imgData];

提前致谢。

【问题讨论】:

  • 你在 jsonDict 中得到了什么:imageURL 或 imageData?
  • 默认 UITableviewCell 有单个图像视图。如果您想对单个单元格使用 2 个图像视图,则必须使用自定义表格视图单元格。
  • 你能显示一个图像视图的图像吗?如果是,那么这是您默认可以获得的。如果您需要更多的单元格图像视图,则需要制作自定义单元格。
  • 是的,它对一张图片的工作真棒。
  • 只需在您的单元格中再添加一个 ImageView 并为两个 ImageView 循环您的 ImageArray 或单独声明。

标签: ios objective-c json image uitableview


【解决方案1】:

我终于达到了目标,在一个 UITableViewCell 中显示两个解析的图像 我使用的代码是

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"MyCell"];
if (cell==nil)
{
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];

    UIImageView *imageView1=[[UIImageView alloc]init];
    imageView1.frame=CGRectMake(0, 0,50,cell.frame.size.height);
    imageView1.tag=1000;
    [cell.contentView addSubview:imageView1];

    UIImageView *imageView2=[[UIImageView alloc]init];
    imageView2.frame=CGRectMake(cell.frame.size.width-50, 0, 50,cell.frame.size.height);
    imageView2.tag=2000;
    [cell.contentView addSubview:imageView2];

}


jsonDict = [jsonArr objectAtIndex:indexPath.row];
NSLog(@"imagess%@",jsonDict);
cell.textLabel.text = [jsonDict objectForKey:@"ImageName"];
cell.textLabel.textAlignment=NSTextAlignmentCenter;



if ([jsonDict objectForKey:@"RefImageData"] == [NSNull null]) {
    // photo is null
    NSData *imgData = [NSData dataWithBase64EncodedString:[jsonDict objectForKey:@"ImageData"]];


    UIImageView *imageView=(UIImageView *)[cell.contentView viewWithTag:1000];
    imageView.image=[UIImage imageWithData:imgData];
}
else {
    // photo isn't null
    NSData *imgData = [NSData dataWithBase64EncodedString:[jsonDict objectForKey:@"ImageData"]];

    NSData *refimage=[NSData dataWithBase64EncodedString:[jsonDict objectForKey:@"RefImageData"]];


    UIImageView *imageView=(UIImageView *)[cell.contentView viewWithTag:1000];
    imageView.image=[UIImage imageWithData:imgData];

    UIImageView *imageView2=(UIImageView *)[cell.contentView viewWithTag:2000];
    imageView2.image=[UIImage imageWithData:refimage];


}
return cell;

【讨论】:

    【解决方案2】:

    您需要创建一个自定义的UITableViewCell 子类,其中包含两个图像视图。然后你可以像在这里一样从你的 json 中分配这两个图像。

    Apple provides an example how you can do that,也有很多教程,搜索“自定义uitableviewcell子类教程”即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-12
      • 1970-01-01
      • 2013-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-13
      相关资源
      最近更新 更多