我查了很多例子并尝试合并,但都没有成功 . 在我的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)
}
}
}
1 回答
首先在
didSelectItemAtIndexPath
中传递selectedItem
的indexPath现在像这样在
prepareForSegue
方法中传递图像现在在您想要传递图像的
StoryView
中声明一个StoryImages
类型的对象,并在viewDidLoad
中使用该对象来分配图像