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
}
}
工作。非常感謝 :) – Jake