How to present a view with popover when pressing the button of a cell of a collectionView?

0

I have a view with a collectionView and it shows me several cells. Each cell has several buttons. In one of the cell buttons when pressed, I want to present another view as a 'popover'.

I can not show the view, if I do it through the storyboard with a segue and prepareforsegue the compiler just tells me that I need an anchor that is fixed. As the button is not fixed (because it depends on each cell?), I change the anchor to the view that the collection has but it does not work because the view always comes out in the same place (top left corner)

I have tried to implement it by means of code within the function:     func collectionView (_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)

and add an addTarget to the cell button so that when you press it call the function that shows view but it does not work.

 @objc func pressButton(button: UIButton) {

self.popoverViewController = self.storyboard?.instantiateViewController(withIdentifier: "ChildInfo") as! MyViewController
self.popoverViewController.modalPresentationStyle = UIModalPresentationStyle.popover
self.popoverViewController.popoverPresentationController!.delegate = self
self.present(self.popoverViewController, animated: true, completion: nil )

}

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

let cell = collectionView.cellForItem(at: indexPath) as! MyCell
cell.indexPath = indexPath.row

cell.pruebaButton.addTarget(self, action: #selector(pressButton(button:)), for: .touchDown)

}
    
asked by Popularfan 17.04.2018 в 17:40
source

1 answer

1

From what I understood, you have a cell with several buttons. You have to bear in mind that if you click on a cell, it is already a button, it will execute the didSelectItem. If what you want is that only the buttons that you have inside each cell work, you have to include the target when you create the cell, not when you have pressed it. And how do you know which button they pressed if all the cells point to the same target? either by putting a tag, or how you have an indexPath in each cell.

To create the cell

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("XXX", forIndexPath: indexPath) as! "XXX"
    //añades algo para identificarlo, pero al botón, por ejemplo
    cell.pruebaButton.tag = indexPath.row
    //añades el target
    cell.pruebaButton.addTarget(self, action: #selector(pressButton(button:)), for: .touchDown)
    return cell
}

In the target of the button, with the tag you will know which cell they have pressed and what you have to do

func pressButton(button:)) {
   //button.tag tiene el row de tu modelo de datos
   //obtienes la información que tengas que obtener
   //pones los popover o muestras lo que quieras mostrar desde este método
}

and so that when you touch the cell outside the buttons and do nothing, the didSelectItemAtIndexPath method leaves it empty

    
answered by 26.05.2018 / 11:43
source