2017-10-17 31 views
1

我有2层结构下面给出:不能够通过适当的图像tableviewcell

struct ProductImage { 
    let id : String 
    let url : URL 
    let isDefault : Bool 
} 

struct Product { 
    let name : String 
    let id : String 
    var images = [ProductImage]() 

    init(name : String, id: String) { 
     self.name = name 
     self.id = id 
    } 

    mutating func add(image: ProductImage) { 
     images.append(image) 
    } 
} 

现在我已经具有一定的细胞上,每个具有图像和名称标签和ID的的CollectionView与它的标签。它也有一个按钮,当我点击它时,collectionview单元格上的图像以及名称和ID将显示在tableviewcell中并存储在一个数组中。返回并点击第二个项目的按钮后,我会看到2个单元格(第一个和第二个单元格)。那我在做这样的...

func SellBtnTapped(_ sender: UIButton) { 

    let indexPath = collectionView?.indexPath(for: ((sender.superview?.superview) as! RecipeCollectionViewCell)) 

    self.photoThumbnail = self.arrayOfURLImages[(indexPath?.row)!] 

    let myVC = storyboard?.instantiateViewController(withIdentifier: "productSellIdentifier") as! sellTableViewController 

    let productObject = productData1[(indexPath?.row)!] 

     if selectedItems == nil { 
      selectedItems = [Product(name:productObject.name, id: productObject.id)] 

     } else { 
      selectedItems?.append(productObject) 

     } 
     myVC.arrProduct = selectedItems 
     navigationController?.pushViewController(myVC, animated: true) } 

但我的问题是我不能够通过正确的图像像我这样的名称和编号。在我的情况下,名称&传递的ID是正确的,但传递的图像是错误的。我该如何解决它...?

编辑:其中两个图像和其它数据被加到阵列JSON的解析被给予像这样

 var theProduct = Product(name: name, id: id, theRate: rate, quantity: qty, sku: skuCode, prdCateg: prodCat, prodDescr: description) 
    if let images = anItem["product_images"] as? [[String:String]] { 
    for image in images { 
    guard let imageId = image["id"], 
    let url1 = image["image"], 
    let isDefault = image["is_default"] else {continue} 
    print(imageId) 
    let productImage = ProductImage(id: imageId, url: URL(string: url1)!, isDefault: isDefault == "1") 
    theProduct.add(image: productImage) //image is added here 
    self.productData1.append(theProduct) 

    let url = URL(string: url1) 
    let data = try? Data(contentsOf: url!) 
    self.arrayOfURLImages.append(UIImage(data: data!)!) 
    self.appDelegate.commonArrayForURLImages = self.arrayOfURLImages 

编辑2:

这是如何我分配tableviewcell中的图像和其他数据。这是cellForRow的代码..(从那里细胞被加载的tableview的..)

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell: sellTableViewCell = tableView.dequeueReusableCell(withIdentifier: "sellProductIdentifier") as! sellTableViewCell 

cell.prdImgView?.image = self.appDelegate.commonArrayForURLImages[indexPath.row]        

    let product = arrProduct?[indexPath.row] 
     cell.produvtNameLabel.text = product?.name 
     cell.rateTextField.text = product?.theRate 

     return cell 
    } 
+0

你试过分配给'self.photoThumbnail'时使用的'indexPath.item'代替'indexPath.row'?行在表视图中,集合视图对项目进行操作,因为可以在一行中包含多个单元格,或者可能没有清晰的行。 – Losiowaty

+0

@ Losiowaty ..那不是问题... – bws

+0

你在哪里调用了你的'add(image:ProductImage)'方法 –

回答

1

我想的问题是图像异步加载, 要下载的图像需要一定的时间,这是可能的一旦下载完成,将错误的图像分配给单元格。

试试这个https://github.com/onevcat/Kingfisher

+0

没有@桑迪..这不是问题...图像在特定索引处没有正确地从结构中发送。一旦达到这个目标,问题就可以得到解决。但是,我不知道如何做到这一点...... – bws