如何使uicollectionview重新加载一次它从Firebase接收数据? [英] How to make uicollectionview reload once it receives data from firebase?

查看:116
本文介绍了如何使uicollectionview重新加载一次它从Firebase接收数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让一个UI collectionView在从firebase接收到数据后重新加载自己。现在只有当我点击刷新按钮时,它才能手动工作。

 类MedalsViewController:UICollectionViewController {

var imagesArray = [String]()
var ()
var identity2 = [String]()//创建一个空数组
var imagesArrayGreyed = [String]()
var identities3 = [String] b $ b 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
//获取用户奖牌
self .identities3 = snapshot.value as![String]
print(self.identities3)
})


imagesArray = [1,2, 3] //添加所有的
identity = [Shield,Tie,Star] //在这里添加所有的身份
identities2 = [Shield,Tie] //选择奖牌用户有。
imagesArrayGreyed = [1Greyed,2Greyed,3Greyed] //在这里添加所有的灰色图像

}

覆盖func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
//处理任何可以重新创建的资源。

$ b $重写func viewWillAppear(animated:Bool){

self.tabBarController?.tabBar.hidden = false
//获得奖牌的尝试来自firebase数据库的数组




$ b覆盖func collectionView(collectionView:UICollectionView,cellForItemAtIndexPath indexPath:NSIndexPath) - > UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cell,forIndexPath:indexPath)

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

// Identifications2是你目前拥有的奖牌,所以它会检查你是否拥有奖牌,如果不是,将显示一个问号。
//检查是否可以实时更新的时间。看看firebase并尝试链接它在这里
//同样看看是否可以添加一个进度条,只显示那些灰色的奖牌


//准备好编辑这个(identityities3.contains(identities [indexPath.row])){
imageView.image = UIImage(named:imagesArray [indexPath.row])


$ {
imageView.image = UIImage(named:imagesArrayGreyed [indexPath.row])//设置默认图片不在身份2
}

返回单元格

}

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

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


$ b $ //从身份数组
@IBAction func sendFakeMedalandReload(sender:UIBarButtonItem){
let databaseRef = FIRDatabase。数据库()。引用()
databaseRef.child(users / \(FIRAuth.auth()!。currentUser!.uid)/ medals)。setValue(identity2)
self.collectionView! .reloadData()
}
}

用户通过比较两个数组获得奖牌(id实体和身份3),以确保他们相符合,如果不是,它会灰显数组中不包含的身份的图像(身份3)



数据在viewDidLoad中作为来自Firebase的快照数据。我打算这样做,每当viewWillAppear被调用时,collectionView将被重新加载。

我试图在viewDidLoad和viewDidAppear中调用self.collectionView!.reloadData(),但无济于事。我怀疑这可能是收到数据的延迟导致一个空白数组(身份3)被加载。

解决方案

您需要在获取数据并将其存储到 DataSource 之后调用 self.collectionView!.reloadData()在您的Firebase的 completionBlock: 中, .observeSingleEventOfType

  databaseRef.child(users)。child在
中获得用户奖牌
self.identities3 = snapshot.value as![String]
self.collectionView!.reloadData()
})


I would like to make a UI collectionView reload itself after it has received data from firebase. Right now it only works manually when I click on the refresh button.

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

What this code does is basically display medals a user has by comparing two arrays (identities and identities3) to make sure they tally if not it will greyed out the images of identities no contained in the array (identities3)

The data is received in viewDidLoad as a snapshot data from firebase. I intend to make it such that the collectionView will reload every time when viewWillAppear is called.

I've attempted to call self.collectionView!.reloadData() in viewDidLoad and viewDidAppear but to no avail. I suspect it could be the delay in which data is received causing a blank array (identities3) to be loaded instead.

解决方案

You need to call self.collectionView!.reloadData() after you have retrieved you data and stored it into your DataSource.

i.e in your completionBlock: of your 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()
    })

这篇关于如何使uicollectionview重新加载一次它从Firebase接收数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆