0

I have a button inside a collection view cell and when pressed I want to go to another view controller and pass a string to that view controller. The only problem I'm having is with passing the data, I don't know how to check from which cell the button was clicked.

extension UserViewController: UICollectionViewDataSource{

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView,
                        numberOfItemsInSection section: Int) -> Int {
        return self.posts.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PostCell", for: indexPath) as! UsersCollectionViewCell
        //cell.post = posts[indexPath.item]
        cell.User_Name.text = "\(self.posts[indexPath.item].firstname!) \(self.posts[indexPath.item].lastname!)"
        cell.Country.text = self.posts[indexPath.item].Country

        //user id is in the posts().uid

        return cell
    }

    //the segue is already made in the storyboard, i am trying to pass the user id in the function below

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "Testing1"{
            var view = segue.destination as! ViewTheAccount

            //user_ID = self.posts[indexPath.item].firstname

        }
    }
}
Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
  • See https://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-a-button-in-a-cell-is-tapped/38941510#38941510 for some approaches; This question is about tableviews, but it is basically the same for collectionviews – Paulw11 Jan 17 '18 at 22:18

3 Answers3

0

Add a string variable to you cell class and give it strValue that you want in cellForRow

// btn action in cell

 @IBAction func btnClicked(_ sender: Any)
 {
    /// access cell and get it's string var , self = cell

    /// here use delegate to push another view controller with self.strValue
 }
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0

Try this (I assume here that only single selection is allowed here, plus I am assuming that the segues are started by selecting a cell):

    if segue.identifier == "Testing1" {
        var view = segue.destination as! ViewTheAccount

        if let itemIndex = collectionView.indexPathsForSelectedItems?.first?.item {
            let selectedItem = self.posts[itemIndex]
            // do here what you need
        }
    }
Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
0

So one way to do this is to send the cell in the delegate call or callback that you are using.

class SomeViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var collectionView:UICollectionView!

collectionView(method that dequeues the cell){
      let yourcell = collection view.dequeue(...) as! SomeCell
      yourcell.somecallback = callback

}

 func callback(cell: UICollectionViewCell){
   //To find out which cell it is just

   let indexPath = collection view.indexPathForCell(cell)
  //YOU NOW know which cell this was sent from.

}

}

class SomeCell: UICollectionViewCell{
   var somecallback:((UICollectionViewCell)->())?

   func didPress(sender: UIButton){
        somecallback(self)
   }
   }