lundi 29 juillet 2019

How to check for multiple cases based on label name?

I have a view controller that contains a collection view on multiple categories. Different data and details including photos related to that category are fetched. I do not know how to make a case of switch statement regarding this. Writing the same code over and over does not seem like a good idea.

I have at least 14 different cells to cover...

Heres what I got

// Fetch posts based on trend name/hashtag
 func fetchPosts() {
    if trendName == "Bike Shorts" {
            print("Fetching posts with the hashtag bikeshorts")

         HASHTAG_POST_REF.child("bikeshorts").observe(.childAdded) { (snapshot) in

                let postId = snapshot.key
                Database.fetchPost(with: postId, completion: { (post) in
                    self.posts.append(post)
                    self.collectionView?.reloadData()
                })


           }

    } else {
        if trendName == "Animal Prints" {
            self.trendDetails = "This is the label for animal prints"
            HASHTAG_POST_REF.child("animalprints").observe(.childAdded) { (snapshot) in

                let postId = snapshot.key
                Database.fetchPost(with: postId, completion: { (post) in
                    self.posts.append(post)
                    self.collectionView?.reloadData()
                })


            }
        }
    } else {
        if self.trendName == "Bermunda Shorts" {
            self.trendDetails = "This is the label for bermunda shorts"
            HASHTAG_POST_REF.child("bikeshorts").observe(.childAdded) { (snapshot) in

                let postId = snapshot.key
                Database.fetchPost(with: postId, completion: { (post) in
                    self.posts.append(post)
                    self.collectionView?.reloadData()
                })


            }
        }
    }


    }

Aucun commentaire:

Enregistrer un commentaire