Segue from UICollectionView in Swift
This question probably has an extremely simple answer, but I can't get it right in my head. I also cannot find a particularly helpful answer that works anywhere online.
I just can't get the segue to work from the UICollectionViewCell. I am perhaps coming to it from the wrong angle as I assumed it worked very similar to a table segue. Working on this premise, if I were to use a table view, it would look something like this:
if segue.identifier == "TripsToPastTripDetailsSegue" {
let newViewController = segue.destinationViewController as! pastTripDetailViewController
let selectedRow: NSManagedObject = tripsList[tableView.indexPathForSelectedRow!.row] as NSManagedObject
newViewController.passedTrip = selectedRow as! Trips
}
So, in a table view, I would pass one object (from Core Data) to another view controller. Just. But I just can't seem to translate this into a collection view.
I am trying something like this:
if segue.identifier == "pastTripCollectionToSavedLocationSegue" {
let newViewController = segue.destinationViewController as! pastTripDetailViewController
let selectedRow: NSManagedObject = locationsList[collectionView.indexPathForCell(pastLocationImageCollectionViewCell)] as! NSManagedObject
newViewController.passedTrip = selectedRow as! Trips
}
But it gives me the error Cannot find element 'indexPathForCell' , so I am obviously doing something very wrong. This is one of the many mistakes I had while playing around with the code.
Can anyone point me in the right direction?
EDIT:
As requested by @Laffen, the UICollectionView appears in the following places in the code:
class pastTripDetailViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, MKMapViewDelegate {
Like IBOutlet:
@IBOutlet weak var collectionView: UICollectionView!
Data source and delegate in ViewDidLoad:
self.collectionView.dataSource = self
self.collectionView.delegate = self
And here are the rest:
func collectionView(collectionView: UICollectionView, numberOfSectionsInCollectionView indexPath: Int) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.locationsList.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let location = self.locationsList[indexPath.row]
let cell: pastLocationImageCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("imageCell", forIndexPath: indexPath) as! pastLocationImageCollectionViewCell
cell.locationImage.image = UIImage(data: location.image!)
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("pastTripCollectionToSavedLocationSegue", sender: self)
}
source to share
Maybe this might work for you:
let indexPath: NSIndexPath = self.collectionView.indexPathsForSelectedItems().first!
let selectedRow: NSManagedObject = locationsList[indexPath] as! NSManagedObject
This will only work if it locationsList
has a dictionary and is initialized like this:
var locationsList = [NSIndexPath: NSManagedObject]()
Hope this is helpful
Edit:
You seem to be trying to walk like pastLocationImageCollectionViewCell
c indexPathForCell
. The argument must be an instance of the type pastLocationImageCollectionViewCell
.
Try the following:
Edited: The example below will only work as desired when there is only one section in the UICollectionView.
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "pastTripCollectionToSavedLocationSegue" {
let newViewController = segue.destinationViewController as! pastTripDetailViewController
let indexPath = sender as! NSIndexPath
let selectedRow: NSManagedObject = locationsList[indexPath.row] as! NSManagedObject
newViewController.passedTrip = selectedRow as! Trips
}
}
// Set the indexPath of the selected item as the sender for the segue
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("pastTripCollectionToSavedLocationSegue", sender: indexPath)
}
source to share
try this code put it in collection class (Swift 4, xcode 9)
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("cell clicked")
let singleDetail = storyboard?.instantiateViewController(withIdentifier: "urDestinationViewControllerIdentifier") as? urDestinationViewControllerClass
singleDetail?.modalPresentationStyle = .fullScreen
self.present(singleDetail!, animated: true) {() -> Void in }
}
source to share