Collection-View-in-a-Table-View-Cell
Collection-View-in-a-Table-View-Cell copied to clipboard
How would I perform segues from the collectionView cells?
Love the code and your tutorial, works great! I have now pulled data from a Parse backend and displayed it in the collectionView cells and I'd like to know how you would perform segues from the cells of the collectionView to a ViewController displaying the cells content. I have been able to do this with didSelectItemAtIndexPath etc. but in order to display the correct data in the destination view controller I need to add some bits into the prepareForSegue function. Specifically I need to reference the collectionView outlet that is established in TableViewCell.swift . How would I reference this outlet in ViewController.swift in order to ready the correct data through the prepareForSegue function?
Hi there! This is a pretty big topic, outside the scope of just this tutorial (which is just focused on UI stuff). I'd recommend Ray Wenderlich and their tutorials for higher-level stuff.
It's tricky – everyone struggles with these concepts at first, but with practice you'll get used to it. Make sure you understand the data source and delegation patterns before moving on. Best of luck!
Hello !
Thank you for your code and tutorial.
I have the exact same question, and i really struggle... Where and how to specify which object was selected to the new view Controller ?
Thank you a lot
http://stackoverflow.com/questions/34486465/how-can-i-segue-from-uicollectionviews-cells-embedded-inside-uitableviewcell-s
Figuring out which object was selected is tricky at first. You have a collection view within a table view, and one of the collection view cells was selected. The callback method is:
public func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
OK, so that gives us the index path of the cell within its collection view, so we're half way there. The next step is we need to figure out which table view contains the cell's collection view. This is handled for us by the tag property. Just like how we set a cell's background colour based on its collection view's tag, we can use the same logic to identify the object that was selected. So our callback might look like:
public func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
...
let objectSelected = model[collectionView.tag][indexPath.item]
...
}
I hope that helps!
where did you put that public function because I tried to put it in my extension on my view controller and it doesn't seem to ever get called.
@techyowl the function needs to go in whatever object will be the collection view's datasource, whatever conforms to UICollectionViewDataSource. In my tutorial, that's the view controller. If it's not getting called maybe the collection view isn't having it's datasource property set?
@ashfurrow i'm having the same issue with didselectitem - I just put this:
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("hello")
}
inside the viewcontroller extension we added, but it is never getting called?
@ashfurrow figured it out - @techyowl you might be having the same issue. I had a button in the cell and when hitting the cell the button was getting clicked, and the cell was not getting selected.