2017-03-01 1 views
1

Ich habe eine UIImageView namens "Symbol" in einer UICollectionViewCell. Ich möchte das Bild in der UIImageView ändern, wenn ich auf die UICollectionViewCell tippe.UICollectionViewCell - ImageViewViewImage ändern

Wie mache ich das? Ich habe den folgenden Code bisher.

import UIKit 

class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UICollectionViewDelegate { 

    @IBOutlet weak var collectionView: UICollectionView! 

    let icons = [UIImage(named: "pa-w"),UIImage(named: "pi-w"),UIImage(named: "po-w"),UIImage(named: "r-w")] 
    let iconsHovered = [UIImage(named: "pa-b"),UIImage(named: "pi-b"),UIImage(named: "po-b"),UIImage(named: "r-b")] 
    let names = ["ABC", "DEF", "GHI", "JKL"] 
    let colors = ["#cccccc", "#eeeeee", "#dddddd","#aaaaaa"] 

    var screenSize: CGRect! 
    var screenWidth: CGFloat! 
    var screenHeight: CGFloat! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     collectionView.delegate = self 
     collectionView.dataSource = self 

     let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
     let width = UIScreen.main.bounds.width 
     layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) 
     layout.itemSize = CGSize(width: width/2, height: width/2) 
     layout.minimumInteritemSpacing = 0 
     layout.minimumLineSpacing = 0 

     collectionView!.collectionViewLayout = layout 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return icons.count 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell 
     cell.icon.image = icons[indexPath.row] 
     cell.name.text = " " + names[indexPath.row] 
     cell.backgroundColor = UIColor().HexToColor(hexString: colors[indexPath.row], alpha: 1.0) 
     return cell 
    } 

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     let cell = collectionView.cellForItem(at: indexPath as IndexPath)! as UICollectionViewCell 

     //This line gives an error saying no ImageView called icon exists. 
     cell.icon.image = iconsHovered[indexPath.row] 
    } 

} 


extension UIColor{ 
    func HexToColor(hexString: String, alpha:CGFloat? = 1.0) -> UIColor { 
     // Convert hex string to an integer 
     let hexint = Int(self.intFromHexString(hexStr: hexString)) 
     let red = CGFloat((hexint & 0xff0000) >> 16)/255.0 
     let green = CGFloat((hexint & 0xff00) >> 8)/255.0 
     let blue = CGFloat((hexint & 0xff) >> 0)/255.0 
     let alpha = alpha! 
     // Create color object, specifying alpha as well 
     let color = UIColor(red: red, green: green, blue: blue, alpha: alpha) 
     return color 
    } 

    func intFromHexString(hexStr: String) -> UInt32 { 
     var hexInt: UInt32 = 0 
     // Create scanner 
     let scanner: Scanner = Scanner(string: hexStr) 
     // Tell scanner to skip the # character 
     scanner.charactersToBeSkipped = NSCharacterSet(charactersIn: "#") as CharacterSet 
     // Scan hex value 
     scanner.scanHexInt32(&hexInt) 
     return hexInt 
    } 
} 

Antwort

1

Versuchen let cell = collectionView.cellForItem(at: indexPath as IndexPath)! as CollectionViewCell statt let cell = collectionView.cellForItem(at: indexPath as IndexPath)! as UICollectionViewCell. Dann sollten Sie Ihre Zelle als CollectionViewCell haben, die gemäß Ihrem Code eine Icon-Eigenschaft hat.

+0

Das hat funktioniert. Vielen Dank :) – Jake

Verwandte Themen