将JSON中的2张图片解析为UITableViewCell

3

我能够将单个图像放入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];

提前致谢。


1
默认的UITableviewCell只有一个ImageView。如果您想在单个单元格中使用2个ImageView,则必须使用自定义Tableview单元格。 - Baby Groot
你能够为一个imageView显示图片吗?如果可以的话,那么这就是默认情况下你可以得到的。如果你需要在单元格上有更多的imageView,你需要制作自定义单元格。 - Ashutosh
是的,对于一张图片来说,它的工作非常出色。 - Code cracker
只需在您的单元格中添加一个ImageView,并循环使用ImageArray为两个ImageView或分别声明。 - Dhrumil
只需在“numberofrowsinsection”委托中返回您的数组计数即可。显示TableView委托方法。 - karthikeyan
显示剩余3条评论
2个回答

2

最终我达到了目标,在单个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;

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

苹果提供了一个示例,说明如何做到这一点,此外还有很多教程可用,只需搜索“自定义uitableviewcell子类教程”即可。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接