【发布时间】:2017-09-28 14:57:22
【问题描述】:
Xcode 9 - 斯威夫特 4
没有在 Firebase 数据上设置权限 - 读/写每个人
我将 json 数据导入 firebase,我的数据看起来像这样..
我正在尝试获取 FireBase 数据库中列出的作业的标题,将标题列表放在一个数组中并放入一个 tableView 中,它不会返回任何内容 我的快速代码看起来像这样..
import UIKit
import FirebaseDatabase
class PostViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var jobPostsTableView: UITableView!
var ref: DatabaseReference?
var databaseHandle: DatabaseHandle = 0
var searchJSONQuery : String = ""
var jobsData = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
jobPostsTableView.delegate = self
jobPostsTableView.dataSource = self
//Set the Firebase Reference
ref = Database.database().reference()
// Retreive the posts and listen for changes
databaseHandle = (ref?.child("positions/title").observe(.childAdded, with: { (snapshot) in
//Code to execute when a child is added under "positions"
//Take the value from the snapshot and add it to the jobsData array
let list = snapshot.value as? String
if let actualList = list {
self.jobsData.append(actualList)
self.jobPostsTableView.reloadData()
}
}))!
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return jobsData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell")
cell?.textLabel?.text = jobsData[indexPath.row]
return cell!
}
}
【问题讨论】:
-
有什么问题?
-
@ibrahim Akar 我已经编辑了我的答案,试试吧
-
非常感谢@matias 的帮助 ..它不仅把我带到了正确的地方,我实际上学到了很多东西 .. 缺少一件事 .. 孩子必须被铸造对于 snapshot.children.allObjects 中的孩子! [数据快照]{
标签: json swift firebase firebase-realtime-database swift4