Ios 将数据传递到另一个视图时,将集合视图图像添加到数组时出现问题
我有一个这样的结构Ios 将数据传递到另一个视图时,将集合视图图像添加到数组时出现问题,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我有一个这样的结构 struct ProductImage { let id : String let url : URL let isDefault : Bool } struct Product { let name : String let id : String var images = [ProductImage]() var theRate : String var quantity: String var sku:
struct ProductImage {
let id : String
let url : URL
let isDefault : Bool
}
struct Product {
let name : String
let id : String
var images = [ProductImage]()
var theRate : String
var quantity: String
var sku: String
var prdCateg: String
var prodDescr: String
init(name : String, id: String, theRate: String, quantity: String, sku: String, prdCateg:String, prodDescr: String) {
self.name = name
self.id = id
self.theRate = theRate
self.quantity = quantity
self.sku = sku
self.prdCateg = prdCateg
self.prodDescr = prodDescr
}
mutating func add(image: ProductImage) {
images.append(image)
}
}
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, theRate: productObject.theRate, quantity: productObject.quantity, sku: productObject.sku, prdCateg: productObject.prdCateg, prodDescr: productObject.prodDescr)]
} else {
selectedItems?.append(productObject)
}
myVC.arrProduct = selectedItems
navigationController?.pushViewController(myVC, animated: true)
}
当我点击collectionview项目上的一个按钮时,我会加载一个tableviewcell,其中包含集合视图单元格中的所有详细信息,如名称、费率等
struct ProductImage {
let id : String
let url : URL
let isDefault : Bool
}
struct Product {
let name : String
let id : String
var images = [ProductImage]()
var theRate : String
var quantity: String
var sku: String
var prdCateg: String
var prodDescr: String
init(name : String, id: String, theRate: String, quantity: String, sku: String, prdCateg:String, prodDescr: String) {
self.name = name
self.id = id
self.theRate = theRate
self.quantity = quantity
self.sku = sku
self.prdCateg = prdCateg
self.prodDescr = prodDescr
}
mutating func add(image: ProductImage) {
images.append(image)
}
}
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, theRate: productObject.theRate, quantity: productObject.quantity, sku: productObject.sku, prdCateg: productObject.prdCateg, prodDescr: productObject.prodDescr)]
} else {
selectedItems?.append(productObject)
}
myVC.arrProduct = selectedItems
navigationController?.pushViewController(myVC, animated: true)
}
但问题是我无法正确传递图像
同样在加载tableviewcell的viewcontroller中,这也是我在cellForRowAt中分配数据的方式
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
cell.remStockLabel.text = product?.quantity
return cell
}
但是每个单元格上collectionview显示的图像不正确。换句话说,不显示来自每个indexpath的正确图像
非常感谢您的帮助…谢谢:)在调用tableview之前,请检查通过此行的图像selectedItems=[Product(名称:productObject.name,id:productObject.id,theRate:productObject.theRate,quantity:productObject.quantity,sku:productObject.sku,prdCateg:productObject.prdCateg,prodDescr:productObject.prodDescr]通过这一行,您将了解实际问题在哪里?它可以帮助您调试。您是否检查了self.appDelegate.commonArrayForURLImages[indexPath.row]@cole上的url?您提到的代码片段用于处理struct Product的数据。要获取图像,我们需要处理struct ProductImage。但是如何才能做到这一点我不确定…在调用tableview之前,请检查传入此行的图像selectedItems=[Product(name:productObject.name,id:productObject.id,theRate:productObject.theRate,quantity:productObject.quantity,sku:productObject.sku,prdCateg:productObject.prdCateg,prodDescr:productObject.prodDescr)]通过这一行,您将了解实际问题在哪里?它可以帮助您调试。您是否检查了self.appDelegate.commonArrayForURLImages[indexPath.row]@cole上的url?您提到的代码片段用于处理struct Product的数据。要获取图像,我们需要处理struct ProductImage。但我不知道怎么做。。。