我很难将图像从集合视图单元格传递到另一个集合视图单元格。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main" , bundle: nil)
let vc = mainStoryboard.instantiateViewController(withIdentifier: "ImageViewController") as! ImageViewController
let imageItem = image[indexPath.row]
vc.images = imageItem.image
self.navigationController?.pushViewController(vc, animated: true)
}
然后在ImageViewController中
class ImageViewController: UIViewController ,UICollectionViewDelegate , UICollectionViewDataSource {
@IBOutlet weak var imageCollectionView: UICollectionView!
var image: string!
override func viewDidLoad() {
super.viewDidLoad()
self.imageCollectionView.reloadData()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return image.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = imageCollectionView.dequeueReusableCell(withReuseIdentifier: "ImageCollectionViewCell", for: indexPath) as! imageCollectionViewCell
let url: URL = NSURL(string: image)! as URL
cell.images.af_setImage(withURL: url)
return cell
}
答案 0 :(得分:0)
导入UIKit
//对于第一个VC
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main" , bundle: nil)
if let vc = mainStoryboard.instantiateViewController(withIdentifier: "ImageViewController") as? ImageViewController
vc.reciveImageFromFirstVC = image[indexPath.row]
self.navigationController?.pushViewController(vc, animated: true)
}
//用于第二页图像视图控制器
class ImageViewController : UIViewController {
var image:String!
var reciveImageFromFirstVC : String? {
didSet {
guard let reciveImg = reciveImageFromFirstVC else {
print("Can't get image for some reason")
return}
self.image = reciveImg
self.imageCollectionView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = imageCollectionView.dequeueReusableCell(withReuseIdentifier: "ImageCollectionViewCell", for: indexPath) as! imageCollectionViewCell
let url: URL = NSURL(string: image)! as URL
cell.images.af_setImage(withURL: url)
return cell
}
}
我希望我能帮助您
谢谢