我有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
}
你試過分配給'self.photoThumbnail'時使用的'indexPath.item'代替'indexPath.row'?行在表視圖中,集合視圖對項目進行操作,因爲可以在一行中包含多個單元格,或者可能沒有清晰的行。 – Losiowaty
@ Losiowaty ..那不是問題... – bws
你在哪裏調用了你的'add(image:ProductImage)'方法 –