【问题标题】:How to transfer/display an image from CollectionView to another ViewController如何将图像从 CollectionView 传输/显示到另一个 ViewController
【发布时间】:2016-07-22 12:11:46
【问题描述】:

我查找了许多示例并尝试合并,但均未成功。在我的 CollectionView(已放置在 ViewController 中)中,我想选择一个单元格并将单元格图像推送到另一个 ViewController。图像已放置在字典数组中。我不确定,我应该如何编辑我的 prepareForSegue 或我的 func collectionView...didSelectItemAtIndexPath。此外,任何与您的代码相关的详细解释都会有所帮助,因为我仍在学习 swift 及其语法。

以下是我认为您需要的所有信息,但如果您需要更多信息,请告诉我:

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

}

附加课程: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)
   }
 }
}

【问题讨论】:

  • 您的 segue 是否与 UICollectionViewCellViewController 连接?
  • 你从哪里显示新的viewController来显示图像??????
  • @Nirav 我的 segue 连接到 ViewController
  • @VishalSonawane 抱歉,您的问题让我有些困惑。

标签: ios swift segue viewcontroller collectionview


【解决方案1】:

先在didSelectItemAtIndexPath中传入selectedItem的indexPath

现在像这样在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
}

【讨论】:

  • 您尝试过我的解决方案吗,如果对您有帮助,请采纳答案。
  • 谢谢 Nirav,看来你很接近了。我只是在:var selectedStory: StoryImages = StoryImages() 上收到一个错误(在调用中缺少参数“dataDictionary”的争论)
  • 你能告诉我StoryImages类的减速吗,这样会更有帮助。
  • 我更新了上面的文本以包含 StoryImages 类,谢谢。
  • 我不得不修改您的 selectedStory 代码,因为 viewDid 加载行中的调用表明它没有图像。所以我把更新放在上面。我不得不将新属性名称更改为故事而不是 selectedStory。但是,应用程序在 prepareforsegue 中崩溃了 - 'let indexPath = sender as! NSIndexPath 声明 CollectionViewCell 无法转换为 NSIndexPath。在 prepareForSegue 中,我将故事更改为 story1。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2016-07-26
  • 1970-01-01
  • 2018-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-03
  • 1970-01-01
相关资源
最近更新 更多