2017-03-01 45 views
1

我在UICollectionViewCell中有一个名为“图标”的UIImageView。当我点击UICollectionViewCell时,我想要更改UIImageView中的图像。UICollectionViewCell - 在水龙头上更改ImageViewImage

我该怎么做?到目前为止,我有以下代码。

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

回答

1

尝试let cell = collectionView.cellForItem(at: indexPath as IndexPath)! as CollectionViewCell,而不是let cell = collectionView.cellForItem(at: indexPath as IndexPath)! as UICollectionViewCell。然后你应该把你的单元格作为CollectionViewCell,根据你的代码,它有一个图标属性。

+0

工作。非常感谢 :) – Jake