如何在 Out-side UIButton Click 上获取 CollectionViewCell indexPath



如何在按钮单击和按钮处于collectionviewCell的一侧时获得collectionViewCellindexPath。我正在尝试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
    }

}

使用按钮创建自定义单元格,以编程方式赋予按钮操作,为按钮和超级视图提供标签值,如下所示。实现按钮操作,您可以获得如下所示的索引。

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

试试这个
你在单元格中设置单元格按钮 项目在索引路径 像这样

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

希望这有帮助!

最新更新