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

时间:2019-07-09 07:07:24

标签: ios swift uicollectionview indexpath

如何在单击按钮时获得indexPath中的collectionViewCell,并且按钮从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)
        }
    }
}

4 个答案:

答案 0 :(得分:0)

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
    }


}

答案 1 :(得分:0)

使用按钮创建自定义单元格,以编程方式给按钮操作,为按钮和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)
}

答案 2 :(得分:0)

尝试一下
您可以像这样在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
}  

答案 3 :(得分:0)

您可以为按钮分配标签,并在按钮操作时从标签中检索索引路径。

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

希望这会有所帮助!