Home > database >  TableViewCell not displaying when using Custom View Cells
TableViewCell not displaying when using Custom View Cells

Time:04-27

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

CodePudding user response:

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.

CodePudding user response:

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)
  • Related