Will Jackson Will Jackson - 1 year ago 1107
iOS Question

Populating UITableViewController With Firebase Data Swift, Xcode 7

I am working with

Xcode 7
. I am totally new to
Swift, Xcode, and Firebase
. I would like to have three
s in my iOS app. The first two
TableView controllers
will need dynamic content and the third TableView controller will need static content. I would like for the second and third TableView controllers to display data based on what is pressed on the previous TableView controller. All of the data will come from my Firebase. I have no idea where to start. Please point me in the right direction! Thank you!

Answer Source

This question is broad, in that it asks how to do three different tasks.

I think you'll be better off getting answers if you only ask for one thing at a time.

I can help you with populating a UITableViewController with Firebase.

class MyTableViewController: UITableViewController {
  // your firebase reference as a property
  var ref: Firebase! 
  // your data source, you can replace this with your own model if you wish
  var items = [FDataSnapshot]() 

  override func viewDidLoad() {
    // initialize the ref in viewDidLoad
    ref = Firebase(url: "<my-firebase-app>/items")

  override func viewDidAppear(animated: Bool) {
    // listen for update with the .Value event
    ref.observeEventType(.Value) { (snapshot: FDataSnapshot!) in

      var newItems = [FDataSnapshot]()

      // loop through the children and append them to the new array
      for item in snapshot.children {
        newItems.append(item as! FDataSnapshot)

      // replace the old array
      self.items = newItems
      // reload the UITableView

This technique uses the .Value event, you can also use .ChildAdded, but then you have to keep track of .ChildChanged, '.ChildRemoved', and .ChildMoved, which can get pretty complicated.

The FirebaseUI library for iOS handles this pretty easily.

dataSource = FirebaseTableViewDataSource(ref: self.firebaseRef, cellReuseIdentifier: "<YOUR-REUSE-IDENTIFIER>", view: self.tableView)

dataSource.populateCellWithBlock { (cell: UITableViewCell, obj: NSObject) -> Void in
  let snap = obj as! FDataSnapshot

  // Populate cell as you see fit, like as below
  cell.textLabel?.text = snap.key as String
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download