skip to Main Content

I have a tableview that implements a custom cell that I made in my app. The table view gets its data from firebase and reloads it once the view appears. For some reason the table view rows dont appear. Why is that?

Here is my code

The tableview implementation methods

extension VendorItemsController:UITableViewDelegate,UITableViewDataSource {


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return VendorItems.Items.count
}

func tableView(_ table: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let cell = table.dequeueReusableCell(withIdentifier:"cell",for: indexPath) as? ItemDisplayCell {
    cell.DescriptionLabel?.text = VendorItems.Items[indexPath.row].itemDescription
    cell.PriceLabel.text = VendorItems.Items[indexPath.row].price
     //   print(cell.PriceLabel.text)
    return cell
    }
    return UITableViewCell()
}

Where I call the delegate methods and reload my data

 override func viewDidLoad() {
   
    
  //  if(auth.currentUser != nil){Progress}
    // Do any additional setup after loading the view.
    //no cnnection to table view omgggg
    VendorItems = Vendor()
    table.delegate = self
    table.dataSource = self
    //table.rowHeight = UITableView.automaticDimension
    
    
}

override func viewDidAppear(_ animated: Bool) {
    //need to chheck if there are any documents here...
    print(VendorItems.Items.count)
    VendorItems.loadItemsData {
        self.table.reloadData()
    }
}

This is the loadItemsMethod in the Vendor class, that is run everytime the view appears

 func loadItemsData(completed: @escaping () -> ()) {
    db.collection("Vendor").document(currentUser.currentUser!.uid).collection("Items").addSnapshotListener {(QuerySnapshot, Error) in
        guard Error == nil else {
            print("Error getting data")
            return completed()
        }
       
        for document in QuerySnapshot!.documents {
            print(document.data())
            let item = Item.init(document.data())
            self.Items.append(item)
        }
        
    }
    
}

This is the class that I inherit from in my table view cellForRowAt method

class ItemDisplayCell: UITableViewCell {

@IBOutlet weak var DescriptionLabel: UILabel!

@IBOutlet weak var PriceLabel: UILabel!

@IBOutlet weak var itemImage: UIImageView!

}

Finally, this is the prototype cell that I am trying to make

enter image description here

Sorry I dont have enough reps yet

3

Answers


  1. Make sure you are setting the Cell Identifier property on your prototype cell in Interface Builder. Also you can set a breakpoint in the tableView: cellForRowAt method to check if a cell is dequeued.

    Login or Signup to reply.
  2. Did you register your cell or not? I can see you have set delegates but not register cell. if you’re using custom cell then you have to register your custom cell.

    if you have created tableview programmatically then register cell like this:

    tableView.register(YourCustomCellName.self, forCellReuseIdentifier: CellId)
    

    or if you have created tableview in storyboard and cell in storyboard or xib register cell like this:

    partnersListTableView.register(UINib(nibName: "YourCustomCellName", bundle: nil), forCellReuseIdentifier: CellId)
    
    Login or Signup to reply.
  3. You are doing a mistakes in your method loadItemsData()
    There is completion block to get it back. you are only returning this completion in error case but not in success case. So, you have to return it here also.

    func loadItemsData(completed: @escaping (_ dataGet: Bool) -> void) {
        db.collection("Vendor").document(currentUser.currentUser!.uid).collection("Items").addSnapshotListener {(QuerySnapshot, Error) in
            guard Error == nil else {
                print("Error getting data")
                completed(false)
                return
            }
            
            for document in QuerySnapshot!.documents {
                print(document.data())
                let item = Item.init(document.data())
                self.Items.append(item)
                completed(true)
            }
        }
    }
    

    Use viewWillAppear() insted of viewDidAppear()

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //need to chheck if there are any documents here...
        VendorItems.loadItemsData { [weak self] dataGet in
            if dataGet {
                self?.table.reloadData()
            } else {
                print("Something went wrong")
            }
            
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search