0

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)
}
}
}
Ojay
  • 61
  • 12

1 Answers1

1

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.

Prettygeek
  • 2,461
  • 3
  • 22
  • 44
  • Thanks for your help. Do i have to do anything to my collectionView didselect function? I'll definitely check out the SU lectures but since I'm using a plist file, I'm a little worried the instructions wont be fit to my needs. – Ojay Jul 27 '16 at 11:59
  • You can trigger segue there if it is not automatically connected. Now You have var Story, You can extract story from plist in prepare for segue and just pass it to destination view controller, this is covered in iTunesU course. If You have found my answer helpful, mark it via green tick as approved:) – Prettygeek Jul 27 '16 at 12:02
  • Thanks, I looked up the iTunes course and it says I need prior knowledge of object c, which i have none :) will that stop me from understanding? – Ojay Jul 27 '16 at 12:16
  • No. You should get basics. Maybe later on it will be more difficult to get. – Prettygeek Jul 27 '16 at 12:16