0

I have my storyboard set up such that

Tab bar controller -> nav bar controller 1, nav bar controller 2 -> CollectionView controller 1, CollectionView controller 2

The content inside my collectionviews are basically database i get from firebase (which is working fine). It's just when I start up my app, I can't see the cells in the first collectionview but I can see the cells in my second collectionview. Also, I added a print statement and a segue when you click on a cell and this actually works on both collectionviews. I just can't see the content (just a label) of my collectionviewcell in the first collectionview (the first tab item).

This is the code for my first tab bar item

import Foundation
import UIKit
import Firebase

class SaleListViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
let rootref = FIRDatabase.database().reference()
let storage = FIRStorage.storage()
var item_names = [String]()
var item_image_urls = [String]()
var item_details = [Dictionary<String, String>]()
var group = DispatchGroup()
var processRunning = false

@IBOutlet weak var collectionView: UICollectionView!

override func viewDidLoad() {
    super.viewDidLoad()
    self.load_data()
}

/**
 Call the firebase function
 */
func load_data(){
    self.group.enter()
    self.processRunning = true
    get_firebase_data(){
        if(self.processRunning){
            self.processRunning = false
            self.group.leave()
        }
    }
    self.group.notify(queue: DispatchQueue.main, execute: {
        print("done")
    })
}
/**
 Get data from firebase database
 - parameter completionHandler: Function to be called once it's finished
 */
func get_firebase_data(completionHandler: () -> ()){
    rootref.child("Sale").observe(.value, with: { (snapshot) in
        if(snapshot.exists()){
            var item_data = snapshot.value! as! Dictionary<String, AnyObject>
            for(type_2_container,item_list) in item_data{
                for(uid_container, item_detail_container) in item_list as! Dictionary<String, AnyObject>{
                    for(uid, item_detail) in item_detail_container as! Dictionary<String, AnyObject>{
                        var item_detail_dict = item_detail as? Dictionary<String,String>
                        var item_detail_temp = Dictionary<String, String>()
                        for key in (item_detail_dict?.keys)!{
                            item_detail_temp[key] = item_detail_dict?[key]
                        }
                        item_detail_temp["user_id"] = uid
                        self.item_details.append(item_detail_temp)
                        if(!self.item_names.contains(item_detail_dict!["item_name"]!)){
                            self.item_names.append(item_detail_dict!["item_name"]!)
                            self.item_image_urls.append(item_detail_dict!["item_image"]!)

                        }
                    }
                }
            }
            print(self.item_details)
            self.collectionView?.reloadData()
            print("data was reloaded")
        }
    })
    completionHandler()
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell:SellcollectViewCellController = collectionView.dequeueReusableCell(withReuseIdentifier: "CellSale", for: indexPath) as! SellcollectViewCellController
    cell.cell_name_sale.text = self.item_details[indexPath.row]["item_name"]
    print(cell.cell_name_sale.text)
    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print("selected")
    self.performSegue(withIdentifier: "toDetail", sender: self.item_details[indexPath.row])
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if(segue.identifier == "toDetail"){
        let nextVC = segue.destination as! ShowDetailsViewController
        let item_details = sender as! Dictionary<String, String>
        nextVC.item_details = item_details
    }
}

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

/**
 Signs out a user when the button linked with the IBAction is clicked.
 - parameter sender: Object that sent the action message
 */

@IBAction func log_out(_ sender: Any) {
    do{
        try! FIRAuth.auth()!.signOut()
        self.performSegue(withIdentifier: "logout", sender: nil)
        print("user logged out!")
    } catch {
        print("error")
    }
}

}

And this is my second tab bar item

import Foundation
import UIKit
import Firebase

class BuyListViewController: UIViewController,UICollectionViewDataSource, UICollectionViewDelegate {

let rootref = FIRDatabase.database().reference()
let storage = FIRStorage.storage()
var item_names = [String]()
var item_image_urls = [String]()
var item_details = [Dictionary<String, String>]()
var group = DispatchGroup()
var processRunning = false

@IBOutlet weak var collectionView: UICollectionView!

override func viewDidLoad() {
    super.viewDidLoad()
    self.load_data()
}



func load_data(){
    self.group.enter()
    self.processRunning = true
    get_firebase_data(){
        if(self.processRunning){
            self.processRunning = false
            self.group.leave()
        }
    }

    self.group.notify(queue: DispatchQueue.main, execute: {
        print("done")
    })
}

func get_firebase_data(completionHandler: () -> ()){
    rootref.child("Buy").observe(.value, with: { (snapshot) in
        print("inside buy firebase")
        if(snapshot.exists()){
            var item_data = snapshot.value! as! Dictionary<String, AnyObject>
            for(type_2_container,item_list) in item_data{
                for(uid_container, item_detail_container) in item_list as! Dictionary<String, AnyObject>{
                    for(uid, item_detail) in item_detail_container as! Dictionary<String, AnyObject>{
                        var item_detail_dict = item_detail as? Dictionary<String,String>
                        var item_detail_temp = Dictionary<String, String>()
                        for key in (item_detail_dict?.keys)!{
                            item_detail_temp[key] = item_detail_dict?[key]
                        }
                        item_detail_temp["user_id"] = uid
                        self.item_details.append(item_detail_temp)
                        if(!self.item_names.contains(item_detail_dict!["item_name"]!)){
                            self.item_names.append(item_detail_dict!["item_name"]!)
                            self.item_image_urls.append(item_detail_dict!["item_image"]!)

                        }
                    }
                }
            }
            print(self.item_details)
            self.collectionView?.reloadData()
            print("data was reloaded")
        }
    })
    completionHandler()
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell:BuycollectViewCellController = collectionView.dequeueReusableCell(withReuseIdentifier: "CellBuy", for: indexPath) as! BuycollectViewCellController
    cell.cell_name_buy.text = self.item_details[indexPath.row]["item_name"]
    print(cell.cell_name_buy.text)
    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print("selected")
    self.performSegue(withIdentifier: "toDetail", sender: self.item_details[indexPath.row])
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if(segue.identifier == "toDetail"){
        let nextVC = segue.destination as! ShowDetailsViewController
        let item_details = sender as! Dictionary<String, String>
        nextVC.item_details = item_details
    }
}

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

@IBAction func log_out(_ sender: Any) {
    do{
        try! FIRAuth.auth()!.signOut()
        self.performSegue(withIdentifier: "logout", sender: nil)
        print("user logged out!")
    } catch {
        print("error")
    }
}

}

This is my custom collection view cell controller for the first tab item

import UIKit

class SellcollectViewCellController: UICollectionViewCell {

    @IBOutlet weak var cell_name_sale: UILabel!

}

This is the second collection view cell controller

import UIKit

class BuycollectViewCellController: UICollectionViewCell {

    @IBOutlet weak var cell_name_buy: UILabel!

}

This is a screenshot of my tab bar controller with the two tab items

Screenshot of the first bar item

David kim
  • 21
  • 3

0 Answers0