0

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:

enter image description here

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

}

dapper
  • 11
  • 3
  • In `menuCollectionViewCell`, I'd put the both informations (to get b&W and the colored image), and override the method `setSelected:animated:`, `setHighlighted:animated:` to switch according to the boolean – Larme Apr 06 '16 at 15:30
  • Thanks for you answer, this is how my cell looks like, maybe you could explain how i can setselected image? – dapper Apr 06 '16 at 15:58
  • I don't speak Swift, so it may not works as given, but you should get the logic: add a `var blackAndWhitePFFile: PFFile!`, `varColoredPFFile: PFFile!`. In `func collectionView(collectionView:cellForItemAtIndexPath:)`, set the both values. Then override `selected` and or `highlighted`, getting inspired there: http://stackoverflow.com/questions/31329972/trying-to-override-selected-in-uicollectionviewcell-swift-for-custom-selection setting the correct value to `cellImage` according to the value. You may also have to set the image when you detect that `blackAndWhitePFFile` has been set. – Larme Apr 06 '16 at 16:14
  • thanks i will look into it =) – dapper Apr 06 '16 at 16:19

0 Answers0