I am showing 4 images in a custom UICollectionViewCell
as black and white images, when
image is highlighted, I want to show another image but in color, as I am trying to do in didHighlightItemAtIndexPath
.
How can I do this any suggestions is much appreciated.
If possible when user release touch on cell the image goes back to being black and white:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! menuCollectionViewCell
// Display the country name
if let value = countries[indexPath.row]["nameEnglish"] as? String {
cell.cellTitle.text = value
}
// Display "initial" flag image
let initialThumbnail = UIImage(named: "question")
cell.cellImage.image = initialThumbnail
// Fetch final flag image - if it exists
if let value = countries[indexPath.row]["blackWhite"] as? PFFile {
let finalImage = countries[indexPath.row]["blackWhite"] as? PFFile
finalImage!.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
cell.cellImage.image = UIImage(data:imageData)
}
}
}
}
return cell
func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! menuCollectionViewCell
if cell.cellImage.highlighted == true {
if let value = countries[indexPath.row]["flag"] as? PFFile {
let finalImage = countries[indexPath.row]["flag"] as? PFFile
finalImage!.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
cell.cellImage.image = UIImage(data:imageData)
}
}
}}
// return cell
}
}
or should i change it in :
// Process collectionView cell selection
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! menuCollectionViewCell
if indexPath.row == 0 {
let currentObject = countries[indexPath.row]
self.performSegueWithIdentifier("CollectionViewToTableView", sender: currentObject)
}
collectionViewCell
class menuCollectionViewCell: UICollectionViewCell {
@IBOutlet var cellTitle: UILabel!
@IBOutlet var cellImage: UIImageView!
func toggleSelected ()
{
if (selected){
backgroundColor = UIColor.orangeColor()
}else {
backgroundColor = UIColor.whiteColor()
}
}
}