3

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 images have been placed in an Array of Dictionaries. 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

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

    var story: StoryImages?

override func viewDidLoad() {
    super.viewDidLoad()
    ImageToStory.image = UIImage(named: (story?.itemImage)!)
}

}

ADDITIONAL CLASS: StoryImages

class StoryImages{

var itemImage: String


init(dataDictionary:Dictionary <String,String>) {
itemImage = dataDictionary["ItemImage"]!
}

class func newStoryImage(dataDictionary:Dictionary<String,String>) -> StoryImages {
return StoryImages(dataDictionary: dataDictionary)
   }
 }
}
Ojay
  • 61
  • 12

1 Answers1

2

First pass the indexPath of selectedItem in didSelectItemAtIndexPath

Now pass the image in prepareForSegue method like this

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "ShowToStory") {
        let cell = sender as! UICollectionViewCell //Change UICollectionViewCell with CustomCell name if you are using CustomCell
        let indexPath = self.collectionView?.indexPathForCell(cell) 
        let story = storyImages[indexPath.row]
        let destVC = segue.destinationViewController as! StoryView
        destVC.selectedStory = story
    }
}  

Now declare one StoryImages types of object in your StoryView where you want to pass image and use that object in viewDidLoad to assign image

var selectedStory: StoryImages?

override func viewDidLoad() {
    super.viewDidLoad()
    ImageToStory.image = selectedStory.itemImage // or use NSData if it contain url string
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • Have you try my solution, Please accept the answer if its help you. – Nirav D Jul 28 '16 at 04:24
  • Thanks Nirav, seems like you're close. I just get one error (Missing arguement for parameter 'dataDictionary' in call) on the:var selectedStory: StoryImages = StoryImages() – Ojay Jul 30 '16 at 15:18
  • Can you show me the deceleration of `StoryImages` Class, that would be more helpful. – Nirav D Jul 31 '16 at 04:52
  • I updated the text above to include the StoryImages class, thanks. – Ojay Jul 31 '16 at 18:21
  • I had to revise your selectedStory code because the call in the viewDid load line stated it didnt have a image. So i placed the update above. I had to change the new property name to story instead of selectedStory. However, the app crashed in the prepareforsegue here - 'let indexPath = sender as! NSIndexPath' stated CollectionViewCell couldn't cast to NSIndexPath. In the prepareForSegue, i change story to story1.Thanks for your help. – Ojay Aug 01 '16 at 12:32
  • In your case it is `itemImage`, You need to use that. Download the image same way you are downloading inside `cellForRowAtIndexPath`. – Nirav D Aug 01 '16 at 12:37
  • I'm still trying to get it to work. The code has no errors before running. But after i select a cell, the app crashes here let indexPath = sender as! NSIndexPath – Ojay Aug 09 '16 at 00:23
  • It states [Could not cast value of type 'CollectionView_Practice.CollectionViewCell' (0x10d709460) to 'NSIndexPath' (0x10df02410).] – Ojay Aug 09 '16 at 02:09
  • so your code worked! However, I guess i didnt read your instructions properly. My app kept crashing because I was calling the performSegue method in the collectionView's "didSelectItemAtIndexPath". This method should only be used when the user is not selecting the cell from the UI. However since I'm selecting the cell from Storyboard, I should only be using the prepareForSegue method. – Ojay Aug 11 '16 at 12:55
  • Welcome mate:) Happy Coding :) – Nirav D Aug 11 '16 at 12:57