18

I have a collectionView of photos and want to pass the photo who was cliked to a detailViewControler.

The collection data come from :

 var timeLineData:NSMutableArray = NSMutableArray ()

I would like to use the prepare for segue method.

My problem is how to get the good indexPath from the cell who was clicked ?

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue == "goToZoom" {
        let zoomVC : PhotoZoomViewController = segue.destinationViewController as PhotoZoomViewController
        let cell = sender as UserPostsCell

        let indexPath = self.collectionView!.indexPathForCell(cell)
        let userPost  = self.timeLineData.objectAtIndex(indexPath!.row) as PFObject
        zoomVC.post = userPost

    }
} 
jmcastel
  • 1,365
  • 7
  • 17
  • 34

5 Answers5

38

The sender argument in prepareForSegue:sender: will be the cell if you connected the segue from the cell. In that case you can get the indexPath from the cell,

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showZoomController" {
       let zoomVC = segue.destinationViewController as PhotoZoomViewController
       let cell = sender as UICollectionViewCell
       let indexPath = self.collectionView!.indexPathForCell(cell)
       let userPost  = self.timeLineData.objectAtIndex(indexPath.row) as PFObject
        zoomVC.post = userPost
    }
} 
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • my zoomVc.post still nil. It seems that the cell is not detected as the sender. Do i have to set something in the storyboard ? I just ctrl drag from the cell to the zoomVc to create a "goToZoom" segue. See my edited post – jmcastel Jan 20 '15 at 14:20
  • @jmcastel -- "It seems that the cell..."? Did you actually log cell to see if it's nil or not; it shouldn't be if you made the segue from the cell. Where is zoomVC.post nil (where did you check it)? Is userPost nil in prepareForSegue? You need to do some troubleshooting to see where the problem lies. – rdelmar Jan 20 '15 at 16:37
  • astonishment i can't println my cell in the prepareForSegue method. zoomVC.post is nil in viewDidLoad. – jmcastel Jan 20 '15 at 21:55
  • What do you mean by, "can't println my cell"? Do you mean it prints as nil? – rdelmar Jan 21 '15 at 00:00
  • That's because the if statement is never entered (I had a typo there). It should be "if segue.identifier == "showZoomController" (or whatever your identifier is) – rdelmar Jan 21 '15 at 00:23
  • If you don't connect the segue to the cell, but just the controller you will need to use @pbasdf 's solution – Adam Aug 19 '15 at 18:27
17

The indexPathsForSelectedItems returns an array of indexPaths (since there might be several items selected) so you need to use:

let indexPaths : NSArray = self.collectionView!.indexPathsForSelectedItems()
let indexPath : NSIndexPath = indexPaths[0] as NSIndexPath

(You should probably test to see whether several items are selected, and handle accordingly).

pbasdf
  • 21,386
  • 4
  • 43
  • 75
  • 1
    i have got an error with let indexPaths : NSArray = self.collectionView.indexPathsForSelectedItems() telling me that UICollectionViewCell' does not have a member named 'indexPathsForSelectedItems' – jmcastel Oct 11 '14 at 13:08
  • UICollectionViewCell? Surely self.collectionView is a UICollectionView? – pbasdf Oct 11 '14 at 13:12
  • yes sure.My collectionView is set up with a UICollectionViewController and a custom cell in a UICollectionViewCell – jmcastel Oct 11 '14 at 13:17
  • Sorry, just to double check - your self.collectionView is actually a UICollectionViewCell (or a subclass thereof)? – pbasdf Oct 11 '14 at 13:21
  • My collectionView is : Optional(; layer = ; contentOffset: {0, 0}; contentSize: {320, 0}> collection view layout: ) – jmcastel Oct 11 '14 at 13:26
  • So your collectionView is a UICollectionView, so why the error saying UICollectionViewCell does not have a member...? You haven't cast it as UITableViewCell somewhere by mistake? – pbasdf Oct 11 '14 at 13:40
  • No :( and it was the reason of my post – jmcastel Oct 11 '14 at 13:51
  • I'm not au fait with Swift, but I think that maybe a ! or ? will help. I'll edit my answer... – pbasdf Oct 11 '14 at 14:13
7

In swift 3:

let index = self.collectionView.indexPathsForSelectedItems?.first

tuandapen
  • 284
  • 4
  • 6
2

Swift 3.0

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == “segueID”{
        if let destination = segue.destination as? YourDestinationViewController{
            let cell = sender as! UICollectionViewCell
            let indexPath = myCollectionView.indexPath(for: cell)
            let selectedData = myArray[(indexPath?.row)!]

            // postedData is the variable that will be sent, make sure to declare it in YourDestinationViewController
            destination.postedData = selectedData
        }
    }
}
Community
  • 1
  • 1
fs_tigre
  • 10,650
  • 13
  • 73
  • 146
-1

Objective-c :

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

        PhotoZoomViewController  *zoomVC   = [segue destinationViewController];
        NSIndexPath  *path    = [self.collectionView indexPathForCell:(sender)];
        NSDictionary *dataObj = [timeLineData objectAtIndex:path.row];

        // In PhotoZoomViewController create NSDictionary with name myDictionary
        // in this line will copy the dataObj to myDictionary
        zoomVC.myDictionary   = dataObj;

}

Swift :

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

       let zoomVC = segue.destinationViewController as PhotoZoomViewController
       let cell = sender as UICollectionViewCell
       let indexPath = self.collectionView!.indexPathForCell(cell)
       let userPost  = self.timeLineData.objectAtIndex(indexPath.row) as PFObject
        zoomVC.post = userPost

} 
Bosoud
  • 158
  • 4
  • 24