0

How to get indexPath of collectionViewCell on Button click and Button is in Out sided from collectionviewCell. I am trying with sender and Superview. But it is not working.

@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)
        }
    }
}
Let's_Create
  • 2,963
  • 3
  • 14
  • 33
Coding
  • 85
  • 1
  • 7

4 Answers4

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
    }


}
0

Create custom cell with button, give button action programatically, give tag values for button & superView like the below. Implement button action, you can able to get index like below.

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)
}
komara
  • 334
  • 1
  • 7
0

Try This
you set your cell button in cellForItemAt indexPath like this

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
}  
Mayank Palotra
  • 127
  • 1
  • 8
0

You can assign a tag to button and retrieve indexpath from the tag in action of the button.

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

Hope this helps!

khush
  • 540
  • 5
  • 16