Click here to Skip to main content
15,036,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How can i get a different numbers of images from collectionCell into scrollView with use firebase? In my project i have three viewController's.

FirstVC - collectionView inside a tableView. CollectionView need for display the main images for scroll their. TableView need for display the name and and to scroll the table. (i.e. for those images which are in FirstVC in collectionCell).

SecondVC - only collectionView. Need for display the albums.

ThirdVC - only scrollView inside viewController. ScrollView need for a scroll images which are located in the images folder in firebase.

So when i click on album in secondVC on collectionCell i would like to see in thirdVC in scrollView images which are located in folder images in firebase for scrolling their. My firebase struct is here:

{ 
  "users" : {
    "Y7EHcJuqQWZrVI1HBmNgccbPhm55" : {
      "name" : "Photos",
      "posts" : {
        "images" : {
          "image" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/gallery%2F06BAB847-7C94-40A6-A9C8-C3A11B3F9C81.png?alt=media&token=4f797093-fbc7-437b-935c-d13be1409d13",
          "image1" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/gallery%2F06BAB847-7C94-40A6-A9C8-C3A11B3F9C81.png?alt=media&token=4f797093-fbc7-437b-935c-d13be1409d13"
        },
        "qwerty" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/gallery%2F06BAB847-7C94-40A6-A9C8-C3A11B3F9C81.png?alt=media&token=4f797093-fbc7-437b-935c-d13be1409d13"
      },
      "posts2" : {
        "images" : {
          "image" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/gallery%2F06BAB847-7C94-40A6-A9C8-C3A11B3F9C81.png?alt=media&token=4f797093-fbc7-437b-935c-d13be1409d13",
          "image1" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/gallery%2F06BAB847-7C94-40A6-A9C8-C3A11B3F9C81.png?alt=media&token=4f797093-fbc7-437b-935c-d13be1409d13"
        },
        "qwerty" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/city2.jpg?alt=media&token=64509e18-9884-4449-a081-c67393a7d82a"
      },
      "posts3" : {
        "images" : {
          "image" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/gallery%2F06BAB847-7C94-40A6-A9C8-C3A11B3F9C81.png?alt=media&token=4f797093-fbc7-437b-935c-d13be1409d13",
          "image1" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/gallery%2F06BAB847-7C94-40A6-A9C8-C3A11B3F9C81.png?alt=media&token=4f797093-fbc7-437b-935c-d13be1409d13",
          "image2" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/gallery%2F06BAB847-7C94-40A6-A9C8-C3A11B3F9C81.png?alt=media&token=4f797093-fbc7-437b-935c-d13be1409d13"
        },
        "qwerty" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/oko1.jpg?alt=media&token=fedea2cb-93b1-496c-9392-0b95f4ada7b5"
      }
    }
  }
}


I got on the firstVC the images in the images branch, I have them displayed on the print, but what do i do is next i can't understand.

My FirstVC:
import UIKit
import Firebase
import SDWebImage

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    var nameArray = [String]()
    var addressArray = [String]()
    var postImage = [String](), postImage2 = [String](), postImage3 = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
    
        loadName()
        loadImages()
    
        tableView.allowsSelection = true

    }
    func loadName() {
    
        FIRDatabase.database().reference().child("users").observe(FIRDataEventType.childAdded, with: {(snapshot) in
        
            let value = snapshot.value! as! NSDictionary
            self.nameArray.append(value["name"] as? String ?? "")
        })
    }
    func loadImages() {
    
        FIRDatabase.database().reference().child("users").observe(FIRDataEventType.childAdded, with: {(snapshot) in
        
            let values = snapshot.value! as! NSDictionary
            let posts = values["posts"] as? NSDictionary //[String: AnyObject]
            let posts2 = values["posts2"] as? NSDictionary //[String: AnyObject]
            let posts3 = values["posts3"] as? NSDictionary //[String: AnyObject]
        
            //self.postImage.append(posts?["qwerty"] as? String ?? "")
//            self.postImage2.append(posts2?["qwerty"] as? String ?? "")
//            self.postImage3.append(posts3?["qwerty"] as? String ?? "")

            if let post1 = posts as? [String: AnyObject] {
                for (_, value) in post1["images"] as! [String: AnyObject] {
                    self.postImage.append(value as! String)

                }
            }
            if let post2 = posts2 as? [String: AnyObject] {
                for (_, value) in post2["images"] as! [String: AnyObject] {
                    self.postImage2.append(value as! String)
                }
            }
            if let post3 = posts3 as? [String: AnyObject] {
                for (_, value) in post3["images"] as! [String: AnyObject] {
                    self.postImage3.append(value as! String)
                }
            }

            print(self.postImage3)

            self.tableView.reloadData()
        })
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "segue2" {
            if let indexPaths = self.tableView.indexPathForSelectedRow {
                let indexPath = indexPaths as NSIndexPath
                let dvc = segue.destination as! CollectionViewController
                let imagesArray = [self.postImage[indexPath.row],
                                   self.postImage2[indexPath.row],
                                   self.postImage3[indexPath.row]]
                dvc.images = imagesArray.filter({ !($0.isEmpty) })

            }
        }
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return nameArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell

        cell.userNameLabel.text = nameArray[indexPath.row]

        let array = [postImage[indexPath.row],
                     postImage2[indexPath.row],
                     postImage3[indexPath.row]]
        cell.postImageArray = array.filter({ !($0.isEmpty) })
        return cell
    }
}


My SecondVC:

import UIKit
import SDWebImage

class CollectionViewController: UICollectionViewController {

    var images = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "segue3" {
            if let indexPaths = self.collectionView?.indexPathsForSelectedItems {
                let indexPath = indexPaths[0] as NSIndexPath
                let dvc = segue.destination as! ViewControllerScrollView
                dvc.images = [images[indexPath.item]]
            }
        }
    }
    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return images.count
    } 
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell2

        cell.imageView.sd_setImage(with: URL(string: images[indexPath.item]))
        return cell
    }
}


My ThirdVC:

import UIKit

class ViewControllerScrollView: UIViewController {

    @IBOutlet weak var scrollView: UIScrollView!

    var images = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        for i in 0..<images.count {
            let imageView = UIImageView()
            imageView.sd_setImage(with: URL(string: images[i]))
            let xPosition = self.view.frame.width * CGFloat(i)
            imageView.frame = CGRect(x: xPosition, y: 0, width: self.scrollView.frame.width, height: self.scrollView.frame.height)

            scrollView.contentSize.width = scrollView.frame.width * CGFloat(i + 1)
            scrollView.addSubview(imageView)
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}


What I have tried:

Not tried, everything inside my code
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900