0

App Structure:

I have a Collection View Controller which is one of the levels of a logo quiz game I have created. If the user guesses the logo, I store this in UserDefaults and retrieve it in the Collection View, which puts a checkmark image onto the cell.

Issue:

Everything is working fine, but when the user returns back from the quiz view to the Collection View Controller, the checkmark is not showing. Although, if I go back to the level menu and then open up the Collection View Controller, the checkmark is showing properly. I have tried solving this issue with dispatch method, but it didn't help! I've also tried the CollectionView.reloadData method, but still, nothing happens!

My Code:

import UIKit


class CollectionViewController: UICollectionViewController {
    
    var array = ["Youtube.jpeg","Facebook.svg.png","Google.png","Burger King.png"]
    var tappedImage = String()
    var associatedString = ""
    var expectedAnswer = ""
    let defaults = UserDefaults.standard
    var indexx = 0
    var checked: Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
   
    
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return array.count
    }
    
    //performSegueWithIdentifier("ShowColor", sender: sender)
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        let ImageView = cell.viewWithTag(1) as! UIImageView
        ImageView.image = UIImage(named: array[indexPath.row])
        let button = cell.viewWithTag(2) as! UIButton
        button.tag = indexPath.row
        button.addTarget(self, action: #selector(sendInformation), for: .touchUpInside)
        let checkImage = cell.viewWithTag(3) as! UIImageView
        var name = array[button.tag]
        if let dotRange = name.range(of: ".") {
            name.removeSubrange(dotRange.lowerBound..<name.endIndex)
            expectedAnswer = name
        }
        
         if defaults.value(forKey: "\(expectedAnswer)") != nil{
         let isChecked = defaults.value(forKey: "\(expectedAnswer)") as! Bool
         if isChecked == true{
            checkImage.image = UIImage(named: "checkmark1.png")
         }
         }

        
        
        return cell
    }
    override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "quizSegue" {
            if let quizViewController = segue.destination as? QuizVC {
                quizViewController.image = self.tappedImage
                quizViewController.expectedAnswer = self.expectedAnswer
            }
        }
    }
    
    func sendInformation(_ sender: UIButton){
        tappedImage = array[sender.tag]
        var name = array[sender.tag]
        if let dotRange = name.range(of: ".") {
            name.removeSubrange(dotRange.lowerBound..<name.endIndex)
            expectedAnswer = name
            print(expectedAnswer)
        }
        self.performSegue(withIdentifier: "quizSegue", sender: nil)
    }
    
    @IBAction func unwindToCollectionView(segue: UIStoryboardSegue){
        
    }

Note: Your answer can be in swift 2, I can easily translate it! I used swift 3. Any help is greatly appreciated!

Community
  • 1
  • 1
Mr. Xcoder
  • 4,719
  • 5
  • 26
  • 44

1 Answers1

0

I have finally solved this after tons of Google searches. On the Collection View Controller:

 override func viewWillAppear(_ animated: Bool) {
        let parent = self.view.superview
        self.view.removeFromSuperview()
        self.view = nil
        // unloads the entire view
        parent?.addSubview(self.view)
        // reloads the view
    }

collectionView.reloadData

was not working because I have a button which on reload caused a:

EXC_BAD INSTRUCTION

I have just forced the view to reload. I don't delete this question because someone else might find it useful... Source code from reload/refresh subViews - setNeedsDisplay doesnt work.

Community
  • 1
  • 1
Mr. Xcoder
  • 4,719
  • 5
  • 26
  • 44