gpt4 book ai didi

ios - 从函数更改 uicollectionViewCell 上的图像

转载 作者:行者123 更新时间:2023-11-30 11:21:08 25 4
gpt4 key购买 nike

我有这个代码:

MainCell.swift:

    @IBOutlet weak var imageView: UIImageView! 
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var cellView: UIView!
@IBOutlet weak var cellViewOption: UIView!
@IBOutlet weak var cellViewOptionFavorite: UIButton!
@IBOutlet weak var favoriteBtn1: UIButton!
@IBOutlet weak var favoriteBtn2: UIButton!
@IBOutlet weak var favoriteBtn3: UIButton!
@IBOutlet weak var favoriteIcon1: UIImageView!
@IBOutlet weak var favoriteIcon2: UIImageView!
@IBOutlet weak var favoriteIcon3: UIImageView!

MainController.swift:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return productsObjectArray.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell10", for: indexPath) as! MainViewCollectionViewCell
cell.titleLabel.text = productsObjectArray[indexPath.item].name
let documentsDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let imageFileName = productsObjectArray[indexPath.item].code
let fullImagePath = documentsDir.appendingPathComponent("GET_PHOTO").path + "/" + imageFileName! + ".jpg"
cell.imageView.image = UIImage(contentsOfFile: fullImagePath)

let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(MainViewControler.rightSwiped))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
cell.addGestureRecognizer(swipeRight)

let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(MainViewControler.leftSwiped))
swipeLeft.direction = UISwipeGestureRecognizerDirection.left
cell.addGestureRecognizer(swipeLeft)

cell.favoriteBtn1.tag = indexPath.item
cell.favoriteBtn1.addTarget(self, action: #selector(favoriteBtnPressed1(_:)), for: .touchUpInside)

cell.favoriteBtn2.tag = indexPath.item
cell.favoriteBtn2.addTarget(self, action: #selector(favoriteBtnPressed2(_:)), for: .touchUpInside)

cell.favoriteBtn3.tag = indexPath.item
cell.favoriteBtn3.addTarget(self, action: #selector(favoriteBtnPressed3(_:)), for: .touchUpInside)

return cell
}

@objc func favoriteBtnPressed1(_ sender: UIButton) {
let productStatus = checkProductFavoriteIsAvailable(favoriteKey: "favoriteProducts1", productId: productsObjectArray[sender.tag].code!)
if productStatus == true {
removeProductFromFavorites(favoriteKey: "favoriteProducts1", productId: productsObjectArray[sender.tag].code!)
changeFavoritesImage(favoriteId: 1, status: 0)
} else {
saveProductToFavorites(favoriteKey: "favoriteProducts1", productId: productsObjectArray[sender.tag].code!)
changeFavoritesImage(favoriteId: 1, status: 1)
}
}

@objc func favoriteBtnPressed2(_ sender: UIButton) {
let productStatus = checkProductFavoriteIsAvailable(favoriteKey: "favoriteProducts2", productId: productsObjectArray[sender.tag].code!)
if productStatus == true {
removeProductFromFavorites(favoriteKey: "favoriteProducts2", productId: productsObjectArray[sender.tag].code!)
changeFavoritesImage(favoriteId: 2, status: 0)
} else {
saveProductToFavorites(favoriteKey: "favoriteProducts2", productId: productsObjectArray[sender.tag].code!)
changeFavoritesImage(favoriteId: 2, status: 1)
}
}

@objc func favoriteBtnPressed3(_ sender: UIButton) {
let productStatus = checkProductFavoriteIsAvailable(favoriteKey: "favoriteProducts3", productId: productsObjectArray[sender.tag].code!)
if productStatus == true {
removeProductFromFavorites(favoriteKey: "favoriteProducts3", productId: productsObjectArray[sender.tag].code!)
changeFavoritesImage(favoriteId: 3, status: 0)
} else {
saveProductToFavorites(favoriteKey: "favoriteProducts3", productId: productsObjectArray[sender.tag].code!)
changeFavoritesImage(favoriteId: 3, status: 1)
}
}

func checkProductFavoriteIsAvailable(favoriteKey: String, productId: String) -> Bool{
var status = false
if let arr = UserDefaults.standard.array(forKey: favoriteKey) as? [String]{
print(arr)
for favoriteProduct in arr{
if productId == favoriteProduct {
status = true
break
}
}
}
return status
}

func saveProductToFavorites(favoriteKey: String, productId: String) {
if var favoriteArray = UserDefaults.standard.stringArray(forKey: favoriteKey) {
if !favoriteArray.contains(productId) {
favoriteArray.append(productId)
UserDefaults.standard.set(favoriteArray, forKey: favoriteKey)
}
} else {
let favoriteArray = [productId]
UserDefaults.standard.set(favoriteArray, forKey: favoriteKey)
}
}

func removeProductFromFavorites(favoriteKey: String, productId: String) {
guard var favoriteArray = UserDefaults.standard.stringArray(forKey: favoriteKey),
let index = favoriteArray.index(of: productId) else { return }
favoriteArray.remove(at: index)
UserDefaults.standard.set(favoriteArray, forKey: favoriteKey)
}

func changeFavoritesImage(favoriteId: Int, status: Int){
if favoriteId == 1 && status == 0 {
favoriteIcon1.image = UIImage(named: "favourites_off_icon.png")
}
if favoriteId == 1 && status == 1 {
favoriteIcon1.image = UIImage(named: "favourites_icon.png")
}

if favoriteId == 2 && status == 0 {
favoriteIcon2.image = UIImage(named: "favourites_off_icon.png")
}
if favoriteId == 2 && status == 1 {
favoriteIcon2.image = UIImage(named: "favourites_icon.png")
}

if favoriteId == 3 && status == 0{
favoriteIcon3.image = UIImage(named: "favourites_off_icon.png")
}
if favoriteId == 3 && status == 1 {
favoriteIcon3.image = UIImage(named: "favourites_icon.png")
}
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let productObject = productsObjectArray[indexPath.item]
showSubViewInContainerViewProductCard(view: "ProductDetailView", object: [productObject], productsFilter: 0, marketProductsFilter: 0, menuFilter: 0, preparationFilter: 0, glutenFilter: 0, backFromProductView: false)
}

changeFavoritesImage 函数中,我想将图像更改为 UICollectionViewCell。错误:

Mam błąd: Use of unresolved identifier 'favoriteIcon3', Use of unresolved identifier 'favoriteIcon2', Use of unresolved identifier 'favoriteIcon1'

我的代码显示产品列表。我的按钮可以将这些产品添加到收藏夹、观看等。我想根据产品的状态更改产品列表中的图片。

如何解决?

最佳答案

按如下方式更改您的方法

func changeFavoritesImage(ofSender sender:UIButton,favoriteId: Int, status: Int){

guard let cell = collectionview.cellForItem(at: IndexPath.init(item: sender.tag, section: 0)) as? MainViewCollectionViewCell else {return}

if favoriteId == 1 && status == 0 {
cell.favoriteIcon1.image = UIImage(named: "favourites_off_icon.png")
}
if favoriteId == 1 && status == 1 {
cell.favoriteIcon1.image = UIImage(named: "favourites_icon.png")
}

if favoriteId == 2 && status == 0 {
favoriteIcon2.image = UIImage(named: "favourites_off_icon.png")
}
if favoriteId == 2 && status == 1 {
cell.favoriteIcon2.image = UIImage(named: "favourites_icon.png")
}

if favoriteId == 3 && status == 0{
cell.favoriteIcon3.image = UIImage(named: "favourites_off_icon.png")
}
if favoriteId == 3 && status == 1 {
cell.favoriteIcon3.image = UIImage(named: "favourites_icon.png")
}
}

并更改按钮操作中的方法调用,如下所示

@objc func favoriteBtnPressed1(_ sender: UIButton) {
let productStatus = checkProductFavoriteIsAvailable(favoriteKey: "favoriteProducts1", productId: productsObjectArray[sender.tag].code!)
if productStatus == true {
removeProductFromFavorites(favoriteKey: "favoriteProducts1", productId: productsObjectArray[sender.tag].code!)
changeFavoritesImage(ofSender:sender,favoriteId: 1, status: 0)
} else {
saveProductToFavorites(favoriteKey: "favoriteProducts1", productId: productsObjectArray[sender.tag].code!)
changeFavoritesImage(ofSender:sender,favoriteId: 1, status: 1)
}
}

@objc func favoriteBtnPressed2(_ sender: UIButton) {
let productStatus = checkProductFavoriteIsAvailable(favoriteKey: "favoriteProducts2", productId: productsObjectArray[sender.tag].code!)
if productStatus == true {
removeProductFromFavorites(favoriteKey: "favoriteProducts2", productId: productsObjectArray[sender.tag].code!)
changeFavoritesImage(ofSender:sender,favoriteId: 2, status: 0)
} else {
saveProductToFavorites(favoriteKey: "favoriteProducts2", productId: productsObjectArray[sender.tag].code!)
changeFavoritesImage(ofSender:sender,favoriteId: 2, status: 1)
}
}

@objc func favoriteBtnPressed3(_ sender: UIButton) {
let productStatus = checkProductFavoriteIsAvailable(favoriteKey: "favoriteProducts3", productId: productsObjectArray[sender.tag].code!)
if productStatus == true {
removeProductFromFavorites(favoriteKey: "favoriteProducts3", productId: productsObjectArray[sender.tag].code!)
changeFavoritesImage(ofSender:sender,favoriteId: 3, status: 0)
} else {
saveProductToFavorites(favoriteKey: "favoriteProducts3", productId: productsObjectArray[sender.tag].code!)
changeFavoritesImage(ofSender:sender,favoriteId: 3, status: 1)
}
}

在上面的代码中,我刚刚更改了您的方法的实现以接受发送者参数,以便我们可以使用它来获取单元格,如代码所示

关于ios - 从函数更改 uicollectionViewCell 上的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51258516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com