hellosheikh
hellosheikh

Reputation: 3015

how to get the information from the cell which is being clicked in collectionview

hello I have implemented a UICollectionView in my controller. There are lots of info I am displaying on each cell. When user clicks the particular cell I want to pass all the info that was in the cell to another controller.

As I am displaying all the data from db in the cells and cells are generating dynamically so I think one way could be to pass just a record id from one cell to this function didSelectItemAtIndexPath and then in the second controller I query again from the database. But I think that would not be the good idea. and also I don't know how can I pass the id or any info here in this function didSelectItemAtIndexPath.

So in short I want to display all the info in another controller that is being displayed in that cell which is being clicked

here is my code

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath
        indexPath: NSIndexPath) -> UICollectionViewCell {

            let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell",
                forIndexPath: indexPath) as! CardsCollectionCell
            cell.usernameLabel.text = (((dict["\(indexPath.item)"] as?NSDictionary)!["Trip"] as?NSDictionary)!["userName"] as?NSString)! as String
            cell.feeLabel.text = (((dict["\(indexPath.item)"] as?NSDictionary)!["Trip"] as?NSDictionary)!["fee"] as?NSString)! as String

             return cell
    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

      }

Upvotes: 0

Views: 1585

Answers (1)

Victor Sigler
Victor Sigler

Reputation: 23451

So let's going to explain by parts, the didSelectItemAtIndexPath delegate method is used to know what UICollectionViewCell was tapped in the UICollectionViewController, you can get the UICollectionViewCell very easily using the method cellForItemAtIndexPath(it's not the same as collectionView(collectionView: UICollectionView, cellForItemAtIndexPath) like in the following code:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    let cell = self.collectionView?.cellForItemAtIndexPath(indexPath)  as! CardsCollectionCell
    // access to any property inside the cell you have.
    // cell.usernameLabel.text
}

So regarding in short I want to display all the info in another controller that is being displayed in that cell which is being clicked

You can use the didSelectItemAtIndexPath method without any problem to launch a manual segue using the method performSegueWithIdentifier to another UIViewController and just in the prepareForSegue pass any data you need to pass to the another UIViewController. See the following code:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
   // the identifier of the segue is the same you set in the Attributes Inspector
   self.performSegueWithIdentifier("segueName", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

   if(segue.identifier == "segueName") {
      // this is the way of get the indexPath for the selected cell
      let indexPath = self.collectionView.indexPathForSelectedRow()
      let row = indexPath.row

      // get a reference to the next UIViewController
      let nextVC = segue.destinationViewController as! NameOfYourViewController
   } 
} 

I hope this help you.

Upvotes: 2

Related Questions