简体   繁体   English

CollectionViewCell设置默认位置

[英]CollectionViewCell set default position

I have this code: 我有以下代码:

@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
    }

My cells in CollectionView have the option to move left and right. 我在CollectionView中的单元格可以左右移动。 I need to reset all left or right buttons after pressing the button favoriteBtn3Pressed. 按下按钮favoriteBtn3Pressed之后,我需要重设所有的左侧或右侧按钮。

The moved cell has buttons with the option of adding to favorites. 移动的单元格具有按钮,可以选择添加到收藏夹。

I tried with this code: 我尝试使用以下代码:

@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
            }
        }
}

But it does not work :( 但这不起作用:(

Does anyone know how to do it? 有人知道怎么做吗?

Can you try to replace 你可以尝试更换

cell.favoriteCellView.frame.origin.x = 0

with

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM