无法从firebase将数据加载到Tableview中

时间:2018-03-13 11:26:20

标签: ios swift uitableview firebase firebase-realtime-database

将我的数据从firebase数据库加载到我的tableview中时出现问题。

我有以下内容。 UItableview控制器,CollegeDetailVC(作为一个表格,主要用于填充tableview控制器),单个模型用于单个" Offer",以及OfferCell,它是tableviews自定义单元格

基本上我想填写一个表单,将该表单的数据发送到firebase并使用来自特定用户的所述数据重新加载tableview。我正在收到打印声明中的数据但我的tableview上没有任何内容。我很抱歉,如果这一切都相当微不足道,但我已经被困了一段时间,任何建议将不胜感激。提前抱歉愚蠢的错误,请告诉我这是否更清楚。三江源!!

exports.addPlayerToMatch = function(uid,matchId){
    return new Promise(function(resolve,reject){
        dbFunctions.getPlayerUids(matchId).then(function(rows){
            playerUids = JSON.parse(rows[0].uids_of_players);
            playerUids.push(uid);
            console.log("new player uids: " +playerUids);
            numberOfPlayers = playerUids.length;
            db.query('UPDATE current_matches SET number_of_players = ?, 
uids_of_players = ? WHERE id = ?' ,[numberOfPlayers,playerUids,matchId], 
function (err, rows) {
                if (err){ console.log("ErrorForAddPlayerToMatch: "+err);}
            });
            resolve(numberOfPlayers);
        }); 
    }); 
};

TableView Controller ..包含某个用户的优惠列表

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?

}

表单样式VC

 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()
                }
            }

        }




    } 




}

 }

}

OfferCell

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)

    }) 

}

firebaseDatabase结构在这里

enter image description here

debugger even though data is there its not being passed into my model

2 个答案:

答案 0 :(得分:1)

cell?.titleLabel.text = offers[indexPath.row].college
cell?.subtitleLabel.text = offers[indexPath.row].headCoach

在您的数据库中,您没有使用loadView方法enter image description here尝试解析的大学电子邮件的值。

在firebase响应中,您会获得以下值: 的 ABC123 下, 的 L6wb_mGe7BJyWVPhriP

请登记数据库。如果在那里缺少这些键,手动添加它,您应该能够找到包含数据的tableView。

请注意,如果您尝试将任何对象推送到firebase并且值(例如对于emailId)为nil,则不会在后端创建它。

因此,请确保您向电子邮件密钥发送了一个值,否则它将无法在firebase中显示

答案 1 :(得分:0)

您在Main.storyboard上的 OfferCell 原型单元格中使用了UITableViewCell“Subtitle”样式,该单元格有自己的标签实现。

这里有两个选项:

1)使用“Subtitle”单元格样式的默认实现:( textLabel和detailTextLabel是“Subtitle”单元格样式的默认标签)。

// 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)使用单元格样式的自定义实现:

Custom cell style

这样,您的商店就会进入自定义单元格。

注意: 使用此实现将从您的单元格中删除titleLabel和subtitleLabel,您需要再次添加它们并应用相应的约束。

希望这会对你有所帮助。

O.M。