我有两个结构。这些数据将添加到名为productData1
的数组中。此数组包含图像以及name
,rate
等一些详细信息。图像和数据name
,rate
等将显示在集合视图中。在collectionview cellForItemAt...
这就是我显示数据的方式..
let productObject = productData1[indexPath.row]
cell.nameLabel.text = productObject.name
cell.rateLabel.text = productObject.theRate
这很好用。而我试图表现出来的图像......
if let data = try? Data(contentsOf: productObject.images[0].url) {// Crashed here..
let img = UIImage(data: data)
self.arrayOfURLImages.append(img!)
cell.recipeImageView.image = self.arrayOfURLImages[indexPath.row]
}
但是当我尝试显示这样的图像时,图像会显示在collectionview
中。但是当我滚动它们时,我在if let data = try? Data(contentsOf: productObject....
行fatal error: Index out of range
说了cellForItemAt...
。希望有人能帮忙......
编辑这是func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath as IndexPath) as! RecipeCollectionViewCell
let productObject = productData1[indexPath.row]
cell.nameLabel.text = productObject.name
cell.rateLabel.text = productObject.theRate
if let data = try? Data(contentsOf: productObject.images[0].url) {
let img = UIImage(data: data)
print(img!)
self.arrayOfURLImages.append(img!)
cell.recipeImageView.image = self.arrayOfURLImages[indexPath.row]
}
cell.sellButton.addTarget(self,action: #selector(SellBtnTapped(_:)),for: .touchUpInside)
return cell
std::vector
}