【发布时间】:2019-11-28 14:53:03
【问题描述】:
我正在尝试在 iOS 上的表格视图中显示用户“帖子”。当用户在我的应用程序中写帖子并点击帖子按钮时,帖子会保存到我的 firebase 实时数据库中,但它没有显示在编写代码的视图控制器上的 tableview 中。我不确定如果它是我的代码或与firebase有关的东西没有响应并显示数据。这是我的代码:
import Foundation
import UIKit
import Firebase
class HomeViewController:UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView:UITableView!
var posts = [Post] ()
override func viewDidLoad() {
super.viewDidLoad()
self.observePosts()
tableView = UITableView(frame: view.bounds, style: .plain)
let cellNib = UINib(nibName: "PostTableViewCell", bundle: nil)
tableView.register(cellNib, forCellReuseIdentifier: "postCell")
view.addSubview(tableView)
tableView.delegate = self
tableView.dataSource = self
tableView.tableFooterView = UIView()
tableView.reloadData()
}
func observePosts() {
let postsRef = Database.database().reference().child("posts")
postsRef.observe(.childAdded, with: { snapshot in
print(snapshot.value)
var tempPosts = [Post]()
for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot,
let dict = childSnapshot.value as? [String:Any],
let author = dict["author"] as? [String:Any],
let uid = author["uid"] as? String,
let fullname = author["username"] as? String,
let photoURL = author["photoURL"] as? String,
let url = URL (string:photoURL),
let text = dict["text"] as? String,
let timestamp = dict["timestamp"] as? Double {
let userProfile = UserProfile(uid: uid, fullname: fullname, photoURL: url)
let post = Post(id: childSnapshot.key, author: userProfile, text: text, timestamp: timestamp)
tempPosts.append(post)
}
}
self.posts = tempPosts
self.tableView.reloadData()
})
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! PostTableViewCell
cell.set(post: posts[indexPath.row])
return cell
}
}
这也有帮助,这是我的数据库规则:
{
"rules" :{
"posts" : {
".write" : true,
".read": true
},
"users" : {
".write" : true,
".read" : true
}
}
}
这是我的PostTableViewCell、Post 类和我的PostTableViewCell.xib 的示例: Post Class PostTableViewCell PostTableViewCell.xib
这也是我的代码在输入纯数据时的样子。
import Foundation
import UIKit
import Firebase
class HomeViewController:UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView:UITableView!
var posts = ["This is a text"]
override func viewDidLoad() {
super.viewDidLoad()
tableView = UITableView(frame: view.bounds, style: .plain)
let cellNib = UINib(nibName: "PostTableViewCell", bundle: nil)
tableView.register(cellNib, forCellReuseIdentifier: "postCell")
view.addSubview(tableView)
tableView.delegate = self
tableView.dataSource = self
tableView.tableFooterView = UIView()
tableView.reloadData()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! PostTableViewCell
return cell
}
}
这就是我运行应用程序时的样子。 TableView Loadout With Sample Data
【问题讨论】:
-
你需要做一些调试。在
self.posts = tempPosts行下,尝试:print("[TEST] \(self.posts.count)")。您将知道 Firebase 是否正在返回数据。 -
另外,不需要 self.tableView.reloadData() 两次(一次在 viewDidLoad 中,一次在你的观察方法中)。
-
你在
cell.set(post: posts[indexPath.row])方法中做了什么?您是否有可能根本不会将信息写入单元 UI?如果当时找到有效的帖子,我会尝试记录。 -
-
@allenhinson214 看来您没有进入 Firebase 观察者内部。抱歉,我从未使用过 Firebase 数据库,我不知道它是如何工作的。您是否检查过您的 Firebase 数据库中是否包含数据?
标签: ios swift firebase firebase-realtime-database