从collectionview传递图像(基于json)

时间:2017-11-27 07:10:50

标签: ios swift uicollectionview

我有两种json响应格式......

 //1           {
                    "product_id": "22",
                    "product_name": "asd",
                    "product_description": "Test",
                    "rating_count": 0,
                    "product_images": [
                        {
                            "id": "973",
                            "image": "http://myappp.com/uploads/products/16_546_4_image",
                            "is_default": "0"
                        },
                        {
                            "id": "988",
                            "image": "http://myappp.com/uploads/products/123_45_67_image",
                            "is_default": "0"
                        },
                        {
                            "id": "989",
                            "image": "http://myappp.com/uploads/products/123_45_6_image",
                            "is_default": "1"
                        }
                    ]
                },

//2                {
                    "product_id": "5",
                    "product_name": "cake",
                    "product_description": "The boss and Anchor Hocking are you doing today hope",
                    "rating_count": 0,
                    "product_images": [
                        {
                            "id": "962",
                            "image": "http://myappp.com/uploads/products/123_546_image",
                            "is_default": "1"
                        }
                    ]
            }

这些与2个collectionview单元格相关联。这里有一个名为product_images的数组,带有一些参数。现在我有一个集合视图,我只显示来自网址的is_default值为1的图片。我这样做......

 for anItem in productData {
  var productImages :[ProductImage] = []  //ProductImage is a struct having parameters myid, url and isDefault

if let images = anItem["product_images"] as? [[String:String]] {

     if let defaultImage = images.first(where: {$0["is_default"] == "1"}) {
                let productImage = ProductImage(myId: defaultImage["id"]!,
                                                url: URL(string: defaultImage["image"]!)!,
                                                isDefault: true)
                productImages.append(productImage)
            } } }

因此,如果集合视图项显示图像,则该图像的is_default值为1.但是每个单元格也将与其他图像相关联,其中is_default值为0,其他参数如product_idproduct_name等。但只有在集合视图中显示的是is_default值为1的图像。

现在我想要实现的是当我点击一个collectionview单元格时,我想要传递给另一个视图,而不仅仅是单元格上显示的图像,还有与is_default值为ZnLogEvent stopLoggingToTranscript 的单元格关联的其他图像所以,如果有2个collectionview单元格,并且每个单元格分别具有第1和第2个json(上面给出)的值,那么在点击第一个collectionviewcell时,我应该能够显示3个图像,并点击第2个collectionviewcell我应该只能显示1张图片。

希望有人能帮忙......

0 个答案:

没有答案