gpt4 book ai didi

ios - CollectionViewCell设置默认位置

转载 作者:行者123 更新时间:2023-11-29 06:02:54 25 4
gpt4 key购买 nike

我有这个代码:

@IBAction func favoriteBtn3Pressed(_ sender: Any) {
visibleFavoriteView = 2
getFavoriteDataToProductView(favoriteKey: "favoriteProducts3")
// TODO: add hide to all open menus after deletion
}


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.rightProductSwiped))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
cell.addGestureRecognizer(swipeRight)

let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(MainViewControler.leftProductSwiped))
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)

let productStatus1 = checkProductFavoriteIsAvailable(favoriteKey: "favoriteProducts1", productId: productsObjectArray[indexPath.item].code!)
let productStatus2 = checkProductFavoriteIsAvailable(favoriteKey: "favoriteProducts2", productId: productsObjectArray[indexPath.item].code!)
let productStatus3 = checkProductFavoriteIsAvailable(favoriteKey: "favoriteProducts3", productId: productsObjectArray[indexPath.item].code!)

if productStatus1 == false {
cell.favoriteIcon1.image = UIImage(named: "favourites_off_icon-1")
} else {
cell.favoriteIcon1.image = UIImage(named: "favourites_icon")
}

if productStatus2 == false {
cell.favoriteIcon2.image = UIImage(named: "favourites_off_icon_2")
} else {
cell.favoriteIcon2.image = UIImage(named: "favourites_icon_2")
}

if productStatus3 == false {
cell.favoriteIcon3.image = UIImage(named: "favourites_off_icon_3")
} else {
cell.favoriteIcon3.image = UIImage(named: "favourites_icon_3")
}

return cell
}

我在 CollectionView 中的单元格可以选择左右移动。我需要在按下按钮 favoriteBtn3Pressed 后重置所有左或右按钮。

移动的单元格具有带有添加到收藏夹选项的按钮。

我尝试使用此代码:

@IBAction func favoriteBtn3Pressed(_ sender: Any) {
visibleFavoriteView = 2
getFavoriteDataToProductView(favoriteKey: "favoriteProducts3")
// TODO: add hide to all open menus after deletion

for cell in favoriteProductCollectionView.visibleCells as! [FavoriteCollectionViewCell] {
UIView.animate(withDuration: 0.4, delay: 0.1, options: .curveEaseInOut, animations: {
cell.favoriteCellView.frame.origin.x = 0
}) { (isCompleted) in
}
}
}

但它不起作用:(

有人知道怎么做吗?

最佳答案

你可以尝试更换吗

cell.favoriteCellView.frame.origin.x = 0

cell.favoriteCellView.frame =  cell.favoriteCellView.frame.offsetBy(dx: -50, dy: 0)

关于ios - CollectionViewCell设置默认位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51375896/

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