为什么更新步进值时我的收藏视图消失了?

时间:2018-12-19 08:45:21

标签: ios swift uicollectionview uistepper

这是我的视图控制器中的简化代码

 class WishListVC: UIViewController {

        @IBOutlet weak var wishListCollectionView: UICollectionView!

        private var products = [Product]()
        private var selectedProduct : Product?

        override func viewDidLoad() {
            super.viewDidLoad()

        }



    //MARK: - cell Delegate
  extension WishListVC : ListProductCellDelegate {

     func addToCartButtonDidTapped(at selectedIndexPath: IndexPath, collectionView: UICollectionView) {

        guard let userOrder = userOrder else {return}
        let selectedProduct = products[selectedIndexPath.item]

        Order.addProductToOrderRealmDatabase(userOrder: userOrder, selectedProduct: selectedProduct)
        wishListCollectionView.reloadData()
        updateBadgeOnCartTabBar()


    }

        func stepperButtonDidTapped(at selectedIndexPath: IndexPath, stepperValue: Int, collectionView: UICollectionView) {

            guard let userOrder = userOrder else {return}
            let selectedProduct = products[selectedIndexPath.item]

            if stepperValue > 0 {
                Product.changeProductQuantityInRealmDatabase(selectedProduct: selectedProduct, quantity: stepperValue)
            } else {
                Order.removeProductFromOrderRealmDatabase(userOrder: userOrder, selectedProduct: selectedProduct)
                Product.changeProductQuantityInRealmDatabase(selectedProduct: selectedProduct, quantity: 0)
            }

            wishListCollectionView.reloadData()

            updateBadgeOnCartTabBar()

        }


    }

    //MARK: - Collection View Data Source 
    extension WishListVC : UICollectionViewDataSource, UICollectionViewDelegate {

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

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

            guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: WishListStoryboardData.CollectionViewIdentifiers.productSliderCell.rawValue, for: indexPath) as? ListProductCell else { return UICollectionViewCell()}


            cell.productData = products[indexPath.item]
            cell.delegate = self
            cell.collectionView = wishListCollectionView



            return cell
        }

    }

这是我的收藏夹视图单元格的代码:

protocol ListProductCellDelegate {
    func addToCartButtonDidTapped(at selectedIndexPath: IndexPath, collectionView : UICollectionView)
    func stepperButtonDidTapped( at selectedIndexPath: IndexPath, stepperValue: Int, collectionView : UICollectionView)
}

class ListProductCell: UICollectionViewCell {

@IBOutlet weak var productImageViewAspectRatio: NSLayoutConstraint!

    @IBOutlet weak var addToCartButton: UIButton!
    @IBOutlet weak var stepper: GMStepper!

    var collectionView : UICollectionView?
    var delegate: ListProductCellDelegate?

    var productData : Product? {
        didSet {
            updateUI()
        }
    }

    @IBAction func addToCartButtonDidPressed(_ sender: UIButton) {

        guard let collectionView = collectionView else {return}
        guard let selectedIndexPath = collectionView.indexPathForView(view: sender) else {return}
        self.delegate?.addToCartButtonDidTapped(at: selectedIndexPath, collectionView: collectionView)
    }

    @IBAction func stepperDidTapped(_ sender: GMStepper) {
        guard let collectionView = self.collectionView else {return}
        guard let selectedIndexPath = collectionView.indexPathForView(view: sender) else {return}
        self.delegate?.stepperButtonDidTapped(at: selectedIndexPath, stepperValue: Int(sender.value), collectionView: collectionView)
    }

    private func updateUI() {
        guard let product = productData else {return}

        stepper.value = Double(product.quantity)

        setLikeButton(product: product)
        setCartAndStepperButton()
    }


    private func setCartAndStepperButton() {

        guard let selectedProduct = productData else {return}

        func showStepperButton(status: Bool) {
            // to decide whether to show stepper or add to cart button.

            stepper.isHidden = !status
            stepper.isEnabled = status

            addToCartButton.isHidden = status
            addToCartButton.isEnabled = !status

        }

        if selectedProduct.quantity == 0 {
            showStepperButton(status: false)
        } else {
            showStepperButton(status: true)
        }


    }


}

我不明白为什么“添加到购物车”消失后第一次点击步进器后,收藏视图会消失。

我的整个代码中没有collectionView.isHidden,但是我不知道为什么我的收藏夹视图会像下面的.gif文件那样消失

http://g.recordit.co/NAEc36MbrM.gif

但是,如果步进器已经显示且其步进器值大于1,那么它将像下面的gif一样消失我的收藏夹视图

http://recordit.co/SLdqf1ztFZ.gif

最小步进值设置为1。

如果我将上述wishListCollectionView.reloadData()方法中的收集视图重载数据stepperButtonDidTapped更改为仅使用wishListCollectionView.reloadItems(at: [selectedIndexPath])仅重载特定单元格中的数据,则将解决问题,但步进值似乎更新速度会变慢,并且看起来会很慢。

我不知道如何跟踪将要执行的最后一行,因此它使我的收藏夹视图消失了。

,如果我使用以下方法在主线程中重新加载数据:

DispatchQueue.main.async {
            self.wishListCollectionView.reloadData()
        }

它不会使集合视图消失,但是如果我编辑单元格索引4,它将像下面的gif一样影响单元格索引1:http://g.recordit.co/6802BJDdtx.gif

我更改了第五个单元格中的数字,但是它将自动更改第二个单元格。

1 个答案:

答案 0 :(得分:0)

注意:

  • 使用Xcode Ui调试工具并检查您的collectionView是隐藏的还是空的
  • 领域是实时数据库,您在数据库中所做的任何更改都将被应用 也在您的数组上(例如products数组)

步进问题的原因是:

 guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: WishListStoryboardData.CollectionViewIdentifiers.productSliderCell.rawValue, for: indexPath) as? ListProductCell else { return UICollectionViewCell()}

,以及您在单元格内使用stepperButtonDidTapped的delegete。 将您的单元格一次整理并存储在数组中,如下所示:

var cellArray:[ListProductCell]=[]
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if (cellArray.count > indexPath.row){
        return cellArray[indexPath.row]
    }else{
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: WishListStoryboardData.CollectionViewIdentifiers.productSliderCell.rawValue, for: indexPath) as? ListProductCell else { return UICollectionViewCell()}
        cell.productData = products[indexPath.item]
        cell.delegate = self
        cell.collectionView = wishListCollectionView
        cellArray.append(cell)
        return cell
    }}