2016-09-18 11 views
0

私は、firebaseからデータを受け取った後でUIコレクションビューを再読み込みさせたいと考えています。今は、リフレッシュボタンをクリックしたときにのみ手動で動作します。firebaseからデータを受け取ったら、uicollectionviewを再ロードする方法は?

class MedalsViewController: UICollectionViewController { 

    var imagesArray = [String]() 
    var identities = [String]() 
    var identities2 = [String]() //Creates an empty array 
    var imagesArrayGreyed = [String]() 
    var identities3 = [String]() 
    let databaseRef = FIRDatabase.database().reference() 
    let userID = FIRAuth.auth()?.currentUser?.uid 

    override func viewDidLoad() { 
     super.viewDidLoad() 


     let userID = FIRAuth.auth()?.currentUser?.uid 
     let databaseRef = FIRDatabase.database().reference() 
     databaseRef.child("users").child(userID!).child("medals").observeSingleEventOfType(.Value, withBlock: { (snapshot) in 
      // Get user medals 
      self.identities3 = snapshot.value as! [String] 
      print (self.identities3) 
     }) 


     imagesArray = ["1", "2", "3"] //Add all the medal images here 
     identities = ["Shield", "Tie", "Star"] //Add all the identities here 
     identities2 = ["Shield", "Tie"] //Choose what medals the user has. 
     imagesArrayGreyed = ["1Greyed","2Greyed","3Greyed"] //Add all the greyed images here 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    override func viewWillAppear(animated: Bool) { 

     self.tabBarController?.tabBar.hidden = false 
     // Attempt at getting medals array from firebase database 

    } 



    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) 

     let imageView = cell.viewWithTag(1) as! UIImageView 

     // Identites2 is the medals you currently own, so it'll check back to see if you own the medal and if not a question mark will be displayed. 
     // Time to check to see if it can update in real time. Check out firebase and try to link it here 
     // Also see if can add a progress bar to only show for those greyed out medals 


     // Get ready to edit this to identities3 which is directly taken from firebase database 
     if (identities3.contains(identities[indexPath.row])) { 
      imageView.image = UIImage(named: imagesArray[indexPath.row]) 

     } 
     else { 
      imageView.image = UIImage(named: imagesArrayGreyed[indexPath.row]) //Set default image not in identities2 
     } 

     return cell 

    } 

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return imagesArray.count 
    } 

    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
     let vc = identities[indexPath.row] 
     let viewController = storyboard?.instantiateViewControllerWithIdentifier(vc) 
     self.navigationController?.pushViewController(viewController!, animated: true) 
     viewController?.title = self.identities[indexPath.row] 
    } 


    // Send a fake medal from the array of identities2 
    @IBAction func sendFakeMedalandReload(sender: UIBarButtonItem) { 
     let databaseRef = FIRDatabase.database().reference() 
     databaseRef.child("users/\(FIRAuth.auth()!.currentUser!.uid)/medals").setValue(identities2) 
     self.collectionView!.reloadData() 
    } 
} 

は基本的に2つの配列と比較することによって、ユーザーが持っているメダルを表示する何このコードがないことさ(アイデンティティをしてidentities3)それは配列に格納されているアイデンティティの画像をグレー表示されますされていない場合、彼らは集計を確認する(identities3)

データは、firebaseのスナップショットデータとしてviewDidLoadで受信されます。 viewWillAppearが呼び出されるたびにcollectionViewがリロードされるようにするつもりです。

私はviewDidLoadとviewDidAppearではself.collectionView!.reloadData()を呼び出そうとしましたが、役に立たなかった。私はそれが空の配列(identities3)が代わりに読み込まれる原因となるデータが受信される遅延かもしれないと思う。

答えて

3

データを取得してDataSourceに保存した後にself.collectionView!.reloadData()に電話する必要があります。中

すなわちあなたのcompletionBlock:あなたFirebase .observeSingleEventOfType

databaseRef.child("users").child(userID!).child("medals").observeSingleEventOfType(.Value, withBlock: { (snapshot) in 
     // Get user medals 
     self.identities3 = snapshot.value as! [String] 
     self.collectionView!.reloadData() 
    }) 
+0

それがデータを受信して​​行われ、自動的にそれを呼び出しの際にどのようにそれを知っていますか? –

+0

ありがとうございました! –

関連する問題