在iOS上显示用户帖子 [英] Displaying a users post on iOS
问题描述
我正在尝试在iOS上的表格视图中显示用户的帖子".当用户编写帖子并在我的应用程序中单击发布"按钮时,该帖子将保存到我的Firebase实时数据库中,但不会显示在写入代码的ViewController的tableview中.我不确定如果它是我的代码或与Firebase有关的内容没有响应并显示数据.这是我的代码:
I am trying to display a users "post" in a table view on iOS. When a users writes a post and hits the post button in my app, the post is saved to my firebase real time data base but it is not displaying in the tableview on the viewcontroller on which the code is written in. I'm not sure if its my code or something to do with firebase not responding and showing the data. Here is my code:
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
}
}
这也有帮助,这是我的数据库规则:
Also is this helps, here are my database rules:
{
"rules" :{
"posts" : {
".write" : true,
".read": true
},
"users" : {
".write" : true,
".read" : true
}
}
}
以下是我的PostTableViewCell
,Post
类和我的PostTableViewCell.xib
的示例: 帖子类 PostTableViewCell PostTableViewCell.xib
Here are examples of my PostTableViewCell
, Post
Class and my 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加载
推荐答案
我将其添加为单独的答案,因为我的第一个答案解决了从Firebase读取的一个可能的问题.现在tableView出现了问题.
I am adding this as a separate answer since my first answer addressed a possible issue reading from Firebase. Now there's an issue with the tableView.
附加的代码是一个完整且可运行的项目-精简起来,而不是PostTableViewCell xib,它实际上是UITableViewCell子类,并且匹配xib-没有其他代码.
The attached code is a complete and runnable project - soup to nuts, other than a PostTableViewCell xib which is literally a UITableViewCell subclass and matching xib - no other code.
我希望您可以以此为模板与您的代码进行比较.
I hope you can use this as a template to compare against your code.
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
class PostClass {
var post_text = ""
}
var tableView:UITableView!
var posts = [PostClass]()
override func viewDidLoad() {
super.viewDidLoad()
self.setupTableView()
//create two posts just to test the tableView
let p0 = PostClass()
p0.post_text = "Hello Row 0"
let p1 = PostClass()
p1.post_text = "And Row 1"
self.posts.append(contentsOf: [p0, p1])
}
func setupTableView() {
tableView = UITableView(frame: view.bounds, style: .plain)
let cellNib = UINib(nibName: "PostTableViewCell", bundle: nil)
tableView.register(cellNib, forCellReuseIdentifier: "postCell")
view.addSubview(tableView)
var layoutGuide:UILayoutGuide!
layoutGuide = view.safeAreaLayoutGuide
tableView.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
tableView.topAnchor.constraint(equalTo: layoutGuide.topAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor).isActive = true
tableView.delegate = self
tableView.dataSource = 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
let post = self.posts[indexPath.row]
cell.textLabel!.text = post.post_text
return cell
}
}
和结果图片
这篇关于在iOS上显示用户帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!