jeudi 30 juin 2016

Collection View Displaying Only One Item From An Array


I have a collection view in a view controller.. There is one problem which i can't figure out. The custom cell in a collection view is displaying one item from an array.

Cant figure out what is missing in the code.. I have used both the delegate and data source method..

Here is the code i am using..

viewDidLoad()
pathRef.observeSingleEventOfType(.ChildAdded, withBlock: { (snapshot) in
            let post = CollectionStruct(key: snapshot.key, snapshot: snapshot.value as! Dictionary<String, AnyObject>)
            self.userCollection.append(post)

            let indexPath =  NSIndexPath(forItem: self.userCollection.count-1, inSection: 0)
            self.collectionView!.insertItemsAtIndexPaths([indexPath])

        })



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

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

    let post = userCollection[indexPath.row]


    if let imageUrl = post.category{

        if imageUrl.hasPrefix("gs://"){

            FIRStorage.storage().referenceForURL(imageUrl).dataWithMaxSize(INT64_MAX, completion: { (data, error) in
                if let error = error {

                    print("Error Loading")
                }
                cell.userImg.image = UIImage.init(data: data!)
            })


        }else if let url = NSURL(string: imageUrl), data = NSData(contentsOfURL: url){

            cell.userImg.image = UIImage.init(data: data)
        }
    }
    return cell
}

I am trying to retrieve images stored in firebase database..


Aucun commentaire:

Enregistrer un commentaire