Ojay
Ojay

Reputation: 61

How to select cell image in CollectionView then segue to ViewController

I have looked up many examples and tried to incorporate but have been unsuccessful. In my CollectionView (That has been placed in a ViewController), I'd like to select a cell and push the cell image to another ViewController. The references to images have been placed in an Array of Dictionaries within a plist file. I'm not sure, how i should edit both my prepareForSegue or my func collectionView...didSelectItemAtIndexPath. Also, any detailed explanation to go along with your code will be helpful as I'm still learning swift and its syntax.

Below is what i think all the information you need but please let me know if you need more:

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

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "ShowToStory") {
    var story = sender as! UICollectionViewCell, indexPath = collectionView.indexPathForCell(story)

}
}

private func initStoryImages() {

var storyArchives = [StoryImages]()
let inputFile = NSBundle.mainBundle().pathForResource("StoryArchive", ofType: "plist")

let inputDataArray = NSArray(contentsOfFile: inputFile!)

for inputItem in inputDataArray as! [Dictionary<String, String>] {

    let storyImage = StoryImages(dataDictionary: inputItem)
    storyArchives.append(storyImage)       
}
  storyImages = storyArchives
}

ADDITIONAL CLASS: CollectionViewCell class

class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var cellImage: UIImageView!

func setStoryImage(item:StoryImages){
cellImage.image = UIImage(named:item.itemImage)
}
}

ADDITIONAL CLASS: UIViewController (Edit_1)

class StoryView: UIViewController{
@IBOutlet weak var ImageToStory: UIImageView!

var story: Story?

override func viewWillAppear(animated: Bool) {
if let story = story {
ImageToStory.image = UIImage(named: story.imageName)
}
}
}

Upvotes: 0

Views: 508

Answers (1)

Prettygeek
Prettygeek

Reputation: 2531

Your story view should have public setter for story image. Then You can use prepareForSegue method to pass data. If You still have problems consider watching some videos from Stanford University iOS programming lecture on iTunes U, it is free and this topic is widely covered there.

Upvotes: 1

Related Questions