如何从位于集合视图单元格内的步进器中获取选定的 IndexPath? [英] How to get selected IndexPath from a stepper located inside a collection view cell?

查看:25
本文介绍了如何从位于集合视图单元格内的步进器中获取选定的 IndexPath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个集合视图,它在集合视图单元格内有步进器,用于增加产品的数量,如下图所示.

I have collection view that has stepper inside the collection view cell used to increase the number of product like the image below.

我需要知道,当我单击集合视图单元格上的步进器时.我怎么知道那个集合视图单元的 indexPath.item ?所以我可以使用视图控制器中选择的 indexPath 修改数据?

I need to know, when I click a stepper on a collection view cell. how do I know the indexPath.item of that collection view cell? so I can modify the data using the selected indexPath in the View controller?

所以如果我在第二个单元格中更改步进器,我将始终得到 indexPath.item = 1

so If I change the stepper in the second cell, I will always get indexPath.item = 1

我之前认为 indexPath 将来自下面的 didSelectItemAt 方法.但是当我点击集合视图单元格内的步进器时,似乎不会触发 didSelectItemAt 方法.

I previously think that the indexPath will come from didSelectItemAt method below. but it seems the didSelectItemAt method will not be triggered when I tap the stepper inside the collection view cell.

  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    }

所以我想我可以从 cellForRowAt IndexPath 获取 indexPath 并使用协议委托模式.这是我做的方法,我得到了错误的indexPath

so I think I can get indexPath from cellForRowAt IndexPath and using protocol delegate pattern. here is the method I made, and I got the wrong indexPath

所以如果我在第二个单元格中更改步进器,我不会总是得到 indexPath.item = 1 ,它可以是 2,3,0 等.

So If I change the stepper in the second cell, I will NOT always get indexPath.item = 1 , It can be 2,3,0 etc.

这是视图控制器代码:

class WishListVC: UIViewController, ListProductCellDelegate {

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

     // method from ListProductCellDelegate
    func stepperButtonDidTapped(at selectedIndexPath: IndexPath, stepperValue: Int) {

        // get selectedIndexPath
        // perform some action based on selectedIndexPath

    }
}

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.indexPath = indexPath // I send the indexPath to the cell.
        cell.delegate = self

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        selectedProduct = products[indexPath.item]
        performSegue(withIdentifier: WishListStoryboardData.SegueIdentifiers.toProductVC.rawValue, sender: nil)
    }


}

这里是集合视图单元格中的代码:

and here is the code in the collection view cell:

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

class ListProductCell: UICollectionViewCell {

    var indexPath: IndexPath?
    var delegate: ListProductCellDelegate?

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

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


    func updateUI() {
        // update the UI in cell.
    }
}

推荐答案

您可以尝试将 tag 属性添加到步进器.因此,当您单击步进器时,您可以收听其选择器并确定调用了哪个步进器.标记值应与项目索引相同.

You can try adding tag property to the stepper. So when you click on the stepper you can listen to its selector and determine which stepper was called. The tag value should be same as the item index.

类似的东西

cell.stepper.tag = indexPath.row

上面的代码应该放在 cellForRowAt 委托函数中.

Above code should go inside the cellForRowAt delegate function.

然后当用户点击步进器时调用一个函数并检查标签值

and then when user taps on the stepper call a function and check the tag value

类似的东西

func stepperClicked(sender) {
    //check sender.tag value
    //Do something here with the tag value
}

这篇关于如何从位于集合视图单元格内的步进器中获取选定的 IndexPath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆