首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为什么我的集合视图在更新Steper值时消失了?

为什么我的集合视图在更新Steper值时消失了?
EN

Stack Overflow用户
提问于 2018-12-19 16:45:21
回答 1查看 275关注 0票数 0

以下是我在视图控制器中的简化代码

代码语言:javascript
复制
 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
        }

    }

下面是我的集合视图单元格的代码:

代码语言:javascript
复制
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

但是如果stepper已经用一些大于1的stepper值显示,那么它将使我的集合视图消失,如下面的gif所示。

http://recordit.co/SLdqf1ztFZ.gif

最小步进器值设置为1。

如果我将上面stepperButtonDidTapped方法中的集合视图reload data wishListCollectionView.reloadData()更改为只使用wishListCollectionView.reloadItems(at: [selectedIndexPath])重新加载特定单元格中的数据,问题就会得到解决,但Steper值的更新似乎会稍微慢一点,而且看起来有点滞后。

我不知道如何跟踪要执行的最后一行,所以它会使我的集合视图消失。

如果我在主线程中使用以下命令重新加载数据:

代码语言:javascript
复制
DispatchQueue.main.async {
            self.wishListCollectionView.reloadData()
        }

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

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

EN

回答 1

Stack Overflow用户

发布于 2018-12-19 17:21:37

注意事项:

  • 使用Xcode Ui调试工具检查您的collectionView是否隐藏或为空
  • Realm是realTime数据库,您在数据库中所做的任何更改也将应用于您的阵列(如products阵列)

产生步进器问题的原因是:

代码语言:javascript
复制
 guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: WishListStoryboardData.CollectionViewIdentifiers.productSliderCell.rawValue, for: indexPath) as? ListProductCell else { return UICollectionViewCell()}

以及在单元格内使用stepperButtonDidTapped委托。解密单元格一次,并将它们存储在数组中,如下所示:

代码语言:javascript
复制
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
    }}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53847478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档