2016-09-18 141 views
1

我具有以下和鳥類的字符串數組和圖像kindsViewController.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 
    } 

回答

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 
} 

} 

然後在您的顯示視圖控制器添加條件句,使得當所選擇的任何=,則該信元需要調用setupThreeImages而不是調用setupImage的。您還需要通過調用cell.image =來設置每個圖像....