【发布时间】:2016-08-22 14:31:22
【问题描述】:
import UIKit
import Firebase
class PendingVC: UIViewController,UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var myTableView: UITableView!
let ref = firebasehelper.firebaseURL()
var data = [[:]]
//MARK: vars
var address:AnyObject!
var postTitle:AnyObject!
override func viewDidLoad() {
super.viewDidLoad()
myTableView.dataSource = self
myTableView.delegate = self
//下面的例子很好用,我得到了它应该看起来的布局,但我想从下面的 firebase 函数生成字典。
/*
self.data = [
[
"firstname": "sallie",
"lastname": "ammy"
],
[
"firstname": "jamie",
"lastname": "brown"
]
]
*/
它应该看起来像这样,我想将数据传递到表格中。我不确定我是否应该循环播放。下面的方式会带来以下错误“致命错误:在展开可选值时意外发现 nil”当我打印它们时变量不是 nil,我会取回数据。
ref.childByAppendingPath("backend/posts").queryOrderedByChild("requestFrom").queryEqualToValue(ref.authData.uid).observeEventType(.ChildAdded, withBlock: {snapshot in
var firstname = snapshot.value["firstname"] as! String
var lastname = snapshot.value["lastname"] as! String
self.data = [
[
"firstname": firstname,
"lastname": lastname
]
]
print(self.data)
})
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! statusPrototypeCell
let object = data[indexPath.row]
cell.firstname.text = object["firstname"] as! String
cell.lastname.text = object["lastname"] as! String
return cell
}
override func viewWillAppear(animated: Bool) {
navigationController?.navigationBarHidden = false
navigationController?.navigationBar.barTintColor = UIColor(red:0.4, green:0.76, blue:0.93, alpha:1.0)
navigationController?.navigationBar.translucent = false
self.title = "Signup"
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
}
}
【问题讨论】:
-
[:]是字典而不是数组