gpt4 book ai didi

ios - 如何将图像从 CollectionView 传输/显示到另一个 ViewController

转载 作者:搜寻专家 更新时间:2023-10-31 22:59:40 25 4
gpt4 key购买 nike

我查阅了很多例子并尝试合并但没有成功。在我的 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)
}
}
}

最佳答案

首先在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
}

关于ios - 如何将图像从 CollectionView 传输/显示到另一个 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38526133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com