Ios 无法将正确的图像传递到tableviewcell
我有两个结构,如下所示:Ios 无法将正确的图像传递到tableviewcell,ios,swift,uitableview,uicollectionview,Ios,Swift,Uitableview,Uicollectionview,我有两个结构,如下所示: 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 =
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)
}
}
现在我有了一个collectionview,上面有一些单元格,每个单元格都有一个图像、一个名称标签和一个id标签。它还有一个按钮,当我点击它时,collectionview单元格上的图像以及名称和id显示在tableviewcell中并存储在数组中。返回并单击第二个collectionview
项目的按钮后,我现在将看到两个单元格(第一个和第二个)。我是这样做的
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那样传递正确的图像。在我的情况下,传递的姓名和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..)的代码
我认为问题在于图像异步加载, 下载图像需要一段时间,下载后可能会将错误的图像分配给单元格
尝试此操作在分配给
self.photo缩略图
时,是否尝试使用indexath.item
而不是indexath.row
?行在表视图中,集合视图对项目进行操作,因为一行中可以有多个单元格,或者可能没有清晰的行。@Losiowaty..这不是问题…在哪里调用了add(image:ProductImage)
Method向所选项目添加图像?@SalmanGhumsani添加(image:ProductImage)在通过json解析以获取特定元素时调用…我已编辑了问题并添加了该片段…不@Sandy..这不是问题所在…特定索引处的图像未从结构正确发送。一旦实现了这一点,问题就可以解决了。但我不确定如何做到这一点。。。
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
}