如何从Firebase中将所有数据检索到Swift中的一个表行?

时间:2016-10-02 15:56:39

标签: ios swift firebase firebase-realtime-database

我有3个文本字段,用户可以在其中输入3个输入。

我希望这3个输入全部显示在同一行中,但现在我的应用程序显示每行中的每个值。

我的JSON由根BilletSalg组成,其中包含tickets,其中包含3个键:PriceTicketNameName。它们的值由用户在另一个viewcontroller中键入。

我希望3个键显示在同一行中,但是现在我得到一行price值,然后是一个ticketName值的新行,最后是第三行值Name

这是我的代码:

import UIKit
import Firebase

  class TestTableViewController: UITableViewController {
var files: [FIRDataSnapshot]! = []
func mini() {
    let ref = FIRDatabase.database().reference().child("BilletSalg")
    let usersRef = ref.child("tickets").childByAutoId().observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in

        self.files.append(snapshot)

        print(snapshot)


})
}

let cellNavn = "cellNavn"

  var holes: [FIRDataSnapshot]! = []


let CoursesRef = FIRDatabase.database().reference().child("BilletSalg")





override func viewDidLoad() {
    super.viewDidLoad()

    CoursesRef.observeEventType(.Value, withBlock: { snapshot in




        self.holes = snapshot.childSnapshotForPath("tickets").children.allObjects as! [FIRDataSnapshot]
    self.tableView.reloadData()

    })

    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: #selector(handleCancel))

    tableView.registerClass(NavnCell.self, forCellReuseIdentifier: cellNavn)




}


func handleCancel() {
    dismissViewControllerAnimated(true, completion: nil)
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return holes.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(cellNavn, forIndexPath: indexPath) as! NavnCell



   // I want 3 labels to display the 3 values typed by the user, so in each Row you can view the 3 values

        cell.detailTextLabel?.text = self.holes[indexPath.row].value as? String


 //        cell.textLabel?.text = self.holes[indexPath.row].value as? String
 //
  //        cell.anotherlabel.text? = self.holes[indexPath.row].value as? String


    return cell

    }

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 72
}





}

1 个答案:

答案 0 :(得分:0)

您应该创建一个新的swift文件UITableViewCell,并且您的tableView原型单元必须是该UITableViewCell的子类。我猜你有一个名为NavnCell的UITableViewCell。然后你应该在tableview中为你的原型单元添加标签(假设它们被命名为firstLabel,secondLabel,thirdLabel)并在你的UITableViewCell.swift文件中添加它们的出口。然后在你的

中查阅它们
  

tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath)

用作cell.firstLabel.text,cell.secondLabel.text等。