Reputation: 1365
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
}
}
Upvotes: 18
Views: 51615
Reputation: 158
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
}
Upvotes: -1
Reputation: 284
In swift 3:
let index = self.collectionView.indexPathsForSelectedItems?.first
Upvotes: 7
Reputation: 10738
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
}
}
}
Upvotes: 2
Reputation: 104082
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
}
}
Upvotes: 38
Reputation: 21536
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).
Upvotes: 17