CollectionViewCell设置默认位置

时间:2018-07-17 07:44:48

标签: ios swift

我有此代码:

@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中的单元格可以左右移动。 按下按收藏夹按钮Btn3Pressed之后,我需要重置所有左或右按钮。

移动的单元格具有按钮,可以选择添加到收藏夹。

我尝试了以下代码:

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

但是它不起作用:(

有人知道怎么做吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试替换

cell.favoriteCellView.frame.origin.x = 0

使用

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