2016-07-22 81 views
3

我查找了很多示例并尝试合并但未成功。在我的CollectionView(已被放置在ViewController中)中,我想选择一个单元格并将单元格图像推送到另一个ViewController。图像已放置在字典数组中。我不知道,我该如何编辑我的prepareForSegue或我的func collectionView ... didSelectItemAtIndexPath。此外,随着您的代码的任何详细解释将会有所帮助,因为我仍然在快速学习它的语法。如何将图像从CollectionView传输/显示到另一个ViewController

下面是我认为你需要的所有信息,但请让我知道如果你需要更多:

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 
} 

额外类别:CollectionViewCell类

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

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

额外类别: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) 
    } 
} 
} 
+1

你的segue与'UICollectionViewCell'或'ViewController'连接吗? –

+0

从哪里显示新的viewController来显示图像?????? –

+0

@Nirav我的segue连接到ViewController – Ojay

回答

1

第一遍selectedItem的indexPath在didSelectItemAtIndexPath

现在通过图像prepareForSegue方法这样

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 
    } 
} 

现在,在您StoryView声明一个StoryImages类型的对象,你想通过图像并用viewDidLoad中的那个对象来分配图像

var selectedStory: StoryImages? 

override func viewDidLoad() { 
    super.viewDidLoad() 
    ImageToStory.image = selectedStory.itemImage // or use NSData if it contain url string 
} 
+0

你有没有尝试我的解决方案,请接受答案,如果它可以帮助你。 –

+0

感谢Nirav,好像你很接近。我只是得到一个错误(在调用中失去参数'dataDictionary'的争论):var selectedStory:StoryImages = StoryImages() – Ojay

+0

你能否告诉我'StoryImages'类的减速,这会更有帮助。 –

相关问题