Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift3 UICollectionViewCell-点击时更改图像查看图像_Swift3 - Fatal编程技术网

Swift3 UICollectionViewCell-点击时更改图像查看图像

Swift3 UICollectionViewCell-点击时更改图像查看图像,swift3,Swift3,在UICollectionViewCell中有一个名为“图标”的UIImageView。我想在点击UICollectionViewCell时更改UIImageView中的图像 我该怎么做?到目前为止,我有以下代码 import UIKit class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UICollectionViewDelegate

在UICollectionViewCell中有一个名为“图标”的UIImageView。我想在点击UICollectionViewCell时更改UIImageView中的图像

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

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

尝试使用
let cell=collectionView.cellForItem(位于:indepath as indepath)!作为CollectionViewCell
而不是
让cell=collectionView.cellForItem(位于:indexPath作为indexPath)!作为UICollectionViewCell
。然后您应该将您的单元格设置为CollectionViewCell,根据您的代码,它具有图标属性