Reputation: 27
Having an issue loading my data from firebase database into my tableview.
I have the following. UItableview controller, CollegeDetailVC (which acts as a form that essentailly fills out the tableview controller), individual model for a singular "Offer", and an OfferCell which is the tableviews custom cell
Basically I want to fill out a form, send the data of that form to firebase and reload the tableview with said data from particular user. I am recieving the data in a print statement but nothing on my tableview. I'm Sorry if this is all rather trivial, but I have been stuck for sometime and any advice will be greatly appreciated. Sorry in advance for stupid mistakes, and let me know if this could be more clear. thankyou!!
Model - Offer.swift
class Offer: NSObject {
var college : String?
var headCoach: String?
var email: String?
var skype: String?
var notes: String?
var state: String?
var uniqueId: String?
}
TableView Controller.. that contains a list of offers of a certain User
import UIKit
import Firebase
class MyOffersVC: UITableViewController{
var offers = [Offer]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
loadData()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.offers.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "OfferCell", for: indexPath) as? MyOfferCell
cell?.titleLabel.text = offers[indexPath.row].college
cell?.subtitleLabel.text = offers[indexPath.row].headCoach
return cell!
}
//to get it back to contact form!. in here we need to load all info for selected row into the detailVC
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let detailVC = self.storyboard!.instantiateViewController(withIdentifier: "CollegeDetailVC") as! CollegeDetailVC
detailVC.offer = offers[indexPath.row]
self.navigationController?.pushViewController(detailVC, animated: true)
}
@IBAction func newItemPressed(_ sender: UIBarButtonItem) {
}
func loadData() {
self.offers.removeAll()
let uid = Auth.auth().currentUser?.uid
let ref = Database.database().reference()
ref.child("Users").child(uid!).child("MyOffers").observeSingleEvent(of: .value) { (snapshot) in
if let offerDict = snapshot.value as? [String:AnyObject] {
for (_,offerElement) in offerDict {
print(offerElement)
let offer = Offer()
offer.college = offerElement["college"] as? String
offer.headCoach = offerElement["Headcoach"] as? String
offer.email = offerElement["Email"] as? String
self.offers.append(offer)
print(offer)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
}
}
The Form style VC
CollegeDetailVC
import UIKit
import Firebase
class CollegeDetailVC: UIViewController, UIPickerViewDelegate,
UIPickerViewDataSource {
@IBOutlet weak var collegeField: UITextField!
@IBOutlet weak var headCoachField: UITextField!
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var skypeField: UITextField!
@IBOutlet weak var notesField: UITextView!
@IBOutlet weak var statePicker: UIPickerView!
var details : MyOfferCell!
var offer: Offer?
let statesArray = ["Alabama","Alaska","Arizona", "Arkansas", "California", "Colorado","Connecticut","Delaware", "Georgia", "Florida", "Hawaii","Idaho", "Illinios", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"]
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.isHidden = true
self.statePicker.delegate = self
self.statePicker.dataSource = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if self.offer != nil {
collegeField.text = self.offer?.college
headCoachField.text = self.offer?.headCoach
}
}
@IBAction func savePressed(_ sender: UIButton) {
let uid = Auth.auth().currentUser?.uid
if offer == nil {
offer = Offer()
}
offer?.college = self.collegeField.text
offer?.headCoach = self.headCoachField.text
offer?.email = self.emailField.text
offer?.skype = self.skypeField.text
offer?.notes = self.notesField.text
let ref = Database.database().reference()
let key = ref.child("MyOffers").childByAutoId().key
let dictionaryOffers = ["College" : offer?.college,
"Email" : offer?.email,
"HeadCoach" : offer?.headCoach,
"Skype" : offer?.skype,
"Notes" : offer?.notes]
let childUpdates = ["Users/MyOffers/\(key)": [dictionaryOffers]]
ref.updateChildValues(childUpdates, withCompletionBlock: { (error, ref) -> Void in
self.navigationController?.popViewController(animated: true)
})
}
}
OfferCell
class MyOfferCell: UITableViewCell {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var subtitleLabel: UILabel!
}
firebaseDatabase structure here
Upvotes: 0
Views: 355
Reputation: 101
You are using UITableViewCell "Subtitle" style on your OfferCell prototype cell on Main.storyboard which has their own label implementation.
You have two options here:
1) Use the default implementation of "Subtitle" cell style: (textLabel and detailTextLabel are default labels for the "Subtitle" cell style).
// Change this lines of code:
cell?.titleLabel.text = offers[indexPath.row].college
cell?.subtitleLabel.text = offers[indexPath.row].headCoach
// With this ones:
cell?.textLabel?.text = offers[indexPath.row].college
cell?.detailTextLabel?.text = offers[indexPath.row].headCoach
2) Use a custom implementation of cell style:
This way, your outlets will take place into your custom cell.
Note: Using this implementation will delete your titleLabel and subtitleLabel from your cell, you will need to add them again and apply the corresponding constraints.
Hope this will help you.
O.M.
Upvotes: 0
Reputation: 240
cell?.titleLabel.text = offers[indexPath.row].college
cell?.subtitleLabel.text = offers[indexPath.row].headCoach
In your database, you dont have values for key college or Email which you are trying to parse in loadView method.
In your firebase response, you get these values: abc123, L6wb_mGe7BJyWVPhriP
Please check in database. Add it manually if these keys are missing there, you should be able to find your tableView with data.
Note that if you are trying to push any object to firebase and the value (say for emailId) is nil, it will not be created in backend.
So make sure you are sending a value to your email key else it wont be visible in firebase
Upvotes: 1