使用Firebase Data Swift填充UITableViewController,Xcode 7 [英] Populating UITableViewController With Firebase Data Swift, Xcode 7

查看:177
本文介绍了使用Firebase Data Swift填充UITableViewController,Xcode 7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Xcode 7 中使用 swift 。我完全是新的 Swift,Xcode和Firebase 。我想在我的iOS应用程序中有三个 UITableViewController s。前两个 TableView控制器将需要动态内容,而第三个TableView控制器将需要静态内容。我希望第二个和第三个TableView控制器能够显示基于前一个TableView控制器上按下的内容的数据。所有的数据将来自我的Firebase。我不知道从哪里开始。请指向正确的方向!谢谢!

解决方案

这个问题很广泛,因为它会问如何执行三个不同的任务。
$ b

我认为如果你一次只要求一件事情,你会更好的获得答案。



我可以帮你使用Firebase填充 UITableViewController

  class MyTableViewController:UITableViewController {
//将您的firebase引用作为属性
var ref:Firebase!
//你的数据源,你可以用你自己的模型替换这个模型
var items = [FDataSnapshot]()

override func viewDidLoad(){
super.viewDidLoad()
//初始化viewDidLoad中的ref $ b $ ref = Firebase(url:< my-firebase-app> / items)
}

覆盖func viewDidAppear(animated:Bool){
super.viewDidAppear(animated)
//使用.Value事件监听更新
ref.observeEventType(.Value){(snapshot :FDataSnapshot!)in

var newItems = [FDataSnapshot]()

//通过子元素循环并将它们追加到新数组
用于快照.children {
newItems.append(item as!FDataSnapshot)
}

//替换旧数组$ b $ self.items = newItems
//重新加载UITableView
self.tableView.reloadData()
}
}
}

这个技巧使用t他 .Value 事件,你也可以使用 .ChildAdded ,但是你必须跟踪 .ChildChanged ,'.ChildRemoved'和 .ChildMoved ,可能会变得非常复杂。



适用于iOS的FirebaseUI库可以非常轻松地处理这个问题。

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

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

//根据需要填充单元格,如下所示
cell.textLabel?.text = snap.key as String
}


I am working with swift in Xcode 7. I am totally new to Swift, Xcode, and Firebase. I would like to have three UITableViewControllers 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!

解决方案

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() {
    super.viewDidLoad()
    // initialize the ref in viewDidLoad
    ref = Firebase(url: "<my-firebase-app>/items")
  }

  override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    // 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
      self.tableView.reloadData()
    }
  } 
}

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
}

这篇关于使用Firebase Data Swift填充UITableViewController,Xcode 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆