如何在外部 UIButton 单击上获取 CollectionViewCell indexPath

How to get CollectionViewCell indexPath on Out-sided UIButton Click

如何在单击按钮时获得 indexPathcollectionViewCell 并且按钮位于 collectionviewCell 的外侧。我正在尝试 senderSuperview。但它不起作用。

@IBAction func btnCancel(_ sender: Any) {
    if let cell = (sender as AnyObject).superview??.superview?.superview?.superview?.superview?.superview as? ImageShowinPopUpCell {
        let indexPath = collectionview.indexPath(for: cell)
        deleteDublicateImage(id: isNilValue(assignValue: dbImageDataModel![(indexPath?.row)!].id))
        collectionview.reloadData()
        if dbImageDataModel.count == 0
        {
            dismiss(animated: true, completion: nil)
        }
    }
}
import UIKit

class ViewController: UIViewController ,UICollectionViewDelegate&UICollectionViewDataSource&UICollectionViewDelegateFlowLayout{

    @IBOutlet weak var collectionView: UICollectionView!

    var currentIndexPath:IndexPath!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        collectionView.delegate = self
        collectionView.dataSource = self

    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        cell.backgroundColor = indexPath.row % 2 == 1 ? .blue : .red
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        currentIndexPath = indexPath
    }

    @IBAction func indexPathAction(_ sender: UIButton) {
        print("Current IndexPath",currentIndexPath)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return collectionView.bounds.size
    }


}

使用按钮创建自定义单元格,以编程方式提供按钮操作,为按钮和 superView 提供标签值,如下所示。 实现按钮操作,你可以得到如下索引。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
cell.button.superview.tag = indexPath.section
cell.button.tag = indexPath.row
cell.button.addTarget(self, action: #selector(self.buttonAction), for: .touchUpInside)
return cell
}

func buttonAction(_ sender:UIButton) {
let indexPath = IndexPath(row: sender.tag, section: sender.superview!.tag)
}

试试这个
你像这样在 cellForItemAt indexPath 中设置你的单元格按钮

cell.btnCancel.tag = indexPath.row
cell.btnCancel.addTarget(self, action:#selector(btnCancel), for:.touchUpInside)

//And catch your value like this
@objc func btnCancel(sender: UIButton)
{
    let index = [sender.tag] as IndexPath 
    let dict = self.yourarray[sender.tag] as! NSDictionary
}  

您可以将标签分配给按钮,并从按钮操作中的标签检索索引路径。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        cell.button.tag = indexPath.row
cell.button.addTarget(self, action: #selector(buttonTapped(sender:)), for: UIControl.Event.touchUpInside)
        return cell
    }


@objc func buttonTapped(sender:UIButton){
        let indexPath = Indexpath(row:sender.tag , section: 0)
        let cell = collectionView.cellForItem(at:indexPath)
    }

希望对您有所帮助!