根据所选字符串在集合视图中显示图像

时间:2016-09-18 22:14:46

标签: ios swift

我在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 }

ActiveSheet.CheckBoxes.Add().LinkedCell = Selection.Address

1 个答案:

答案 0 :(得分:0)

以编程方式执行此操作非常容易,您只需要在集合视图单元格中调整图像的锚点,根据选择,您将调用设置1个图像的函数或设置所有图像的函数

设置图像锚点:

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
}

}

然后在你的显示视图控制器中添加条件,这样当selected = any时,单元格需要调用setupThreeImages而不是调用setupImage。你还需要通过调用cell.image = ....

来设置每个图像