我在 kindsViewController.swift
如果从动物数组中选择“Any”,如何在 Collection View 中显示所有动物图像,如果从鸟类数组中选择“Any”,如何显示鸟类图像
这是我目前所做的
class kindsViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var titleDisplayed: UINavigationItem!
var kind = [String]()
var kindImages = [UIImage]()
let animals = ["Dog","Cat","Rabbit", "Any"]
let birds = ["Hen","Kiwi","Parrot", "Any"]
let animalImages: [UIImage] = [UIImage(named: "imageDog")!, UIImage(named: "imageCat")!,UIImage(named: "imageRabbit")!]
let birdImages: [UIImage] = [UIImage(named: "imageHen")!,UIImage(named: "imageKiwi")!,UIImage(named: "imageParrot")!]
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showOptions"
{
let indexPaths = self.collectionView!.indexPathsForSelectedItems!
let indexPath = indexPaths[0] as IndexPath
let AVC = segue.destination as! DisplayViewController
AVC.labelSelected = self.kind [(indexPath as NSIndexPath).row]
// AVC.imageSelected = animalImages
}
}
下面是DisplayViewController.swift
class DisplayViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{
var labelSelected = String()
var imageSelected = [UIImage]()
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
as! DisplayCollectionViewCell
cell.imageView?.image = imageSelected[(indexPath as NSIndexPath).row]
return cell
}
最佳答案
以编程方式执行此操作非常简单,您只需调整 Collection View 单元格中图像的 anchor ,具体取决于选择,您将调用设置 1 个图像或设置所有图像的函数.
设置图像 anchor :
class collectionViewCell : UICollectionViewCell {
let image = UIImageView()
let image2 = UIImageView()
let image3 = UIImageView()
func setupImage () {
view.addSubview( image )
image.translatesAutoResizingMaskIntoConstraints = false
image.topAnchor.constraintEqualToAnchor(view.topAnchor).active = true
image.leftAnchor.constraintEqualToAnchor(view.leftAnchor).active = true
image.widthAnchor.constraintEqualToAnchor(view.widthAnchor).active = true
image.heightAnchor.constraintEqualToAnchor(view.heightAnchor).active = true
}
func setupThreeImages () {
view.addSubview( image )
view.addSubview( image2 )
view.addSubview( image3 )
image.translatesAutoResizingMaskIntoConstraints = false
image.topAnchor.constraintEqualToAnchor(view.topAnchor).active = true
image.leftAnchor.constraintEqualToAnchor(view.leftAnchor).active = true
image.widthAnchor.constraintEqualToAnchor(view.widthAnchor, multiplier : 0.5).active = true
image.heightAnchor.constraintEqualToAnchor(view.heightAnchor).active = true
image2.translatesAutoResizingMaskIntoConstraints = false
image2.topAnchor.constraintEqualToAnchor(view.topAnchor).active = true
image2.leftAnchor.constraintEqualToAnchor(image.rightAnchor).active = true
image2.widthAnchor.constraintEqualToAnchor(view.widthAnchor, multiplier : 0.5).active = true
image2.heightAnchor.constraintEqualToAnchor(view.heightAnchor, multiplier : 0.5).active = true
image3.translatesAutoResizingMaskIntoConstraints = false
image3.topAnchor.constraintEqualToAnchor(image2.bottomAnchor).active = true
image3.leftAnchor.constraintEqualToAnchor(view.rightAnchor).active = true
image3.widthAnchor.constraintEqualToAnchor(view.widthAnchor, multiplier : 0.5).active = true
image3.heightAnchor.constraintEqualToAnchor(view.heightAnchor, multiplier : 0.5).active = true
}
}
然后在您的显示 View Controller 中添加条件,以便当 selected = any 时,单元格需要调用 setupThreeImages 而不是调用 setupImage。您还需要通过调用 cell.image = ... 来设置每个图像。
关于ios - 根据所选字符串在 Collection View 中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39563174/