我在尝试检索之前从表格 View 中选择的用户的帖子时遇到问题。我能够检索用户信息,例如姓名、个人资料照片等,但我很难检索到他们的帖子。目前我的代码没有返回任何东西。我已经在下面发布了我的代码。我遇到问题的地方是我调用函数 loadUserPosts()
的 guestPhotoViewController
。任何帮助将不胜感激,提前致谢!
tableview controller
@IBOutlet weak var searchTableView: UITableView!
var profiles = [user]()
var ref: FIRDatabaseReference!
override func viewdidload(){
ref = FIRDatabase.database().reference()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "guestSegue", sender: self)
self.searchTableView.deselectRow(at: indexPath as IndexPath, animated: true)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "guestSegue" {
if let indexpath = searchTableView.indexPathForSelectedRow {
let tabBarController = segue.destination as! UITabBarController
let guestVC = tabBarController.viewControllers![0] as! guestProfileViewController
_ = tabBarController.viewControllers![1] as! guestPhotoViewController
_ = tabBarController.viewControllers![2] as! guestVideoViewController
guestVC.ref = profiles[indexpath.row].ref
print(indexpath)
}
}
}
guestPhotoViewController - 这是我尝试从 firebase 检索帖子的地方
class guestPhotoViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var backgroundProfileImage: UIImageView!
@IBOutlet weak var profileImage: roundImage!
@IBOutlet weak var fullNameLabel: UILabel!
@IBOutlet weak var postCell: UITableViewCell!
@IBOutlet weak var postTableView: UITableView!
var ref: FIRDatabaseReference!
var posts = [Post]()
override func viewDidLoad() {
super.viewDidLoad()
loadUserPosts()
ref = FIRDatabase.database().reference()
}
func loadUserPosts(){
if let ref = ref {
let postsRef = ref.child("post")
postsRef.observe(.value, with: { (snapshot) in
for post in snapshot.children {
let post = Post(snapshot: post as! FIRDataSnapshot)
self.posts.append(post)
print(post)
self.postTableView.reloadData()
}
})
}
}
帖子数组
class Post{
var profilePic: String!
var fullName: String!
var postImage: String!
var postCaption: String!
var date: NSNumber!
init(profilePic: String, fullName:String, postImage:String, postCaption: String, date:NSNumber){
self.profilePic = profilePic
self.fullName = fullName
self.postImage = postImage
self.postCaption = postCaption
self.date = date
}
init(snapshot:FIRDataSnapshot) {
self.profilePic = (snapshot.value! as! NSDictionary)["profilePic"] as! String!
self.date = (snapshot.value! as! NSDictionary)["dateOfPost"] as! NSNumber!
self.fullName = (snapshot.value! as! NSDictionary)["fullName"] as! String!
self.postImage = (snapshot.value! as! NSDictionary)["postedPic"] as! String!
self.postCaption = (snapshot.value! as! NSDictionary)["postCaption"] as! String!
}
最佳答案
一些事情:
1:如果重写,记得在应用时调用super.method
。
2:不清楚失败的是请求还是更新 TableView 以显示数据。
此外,在 guestPhotoViewController
中,您似乎在设置引用之前调用 loadUserPosts
。你不应该反过来做吗?先设置引用再提出请求?否则,您的 if let ref = ref
将始终失败。
编辑:至于你的新问题,这似乎是关于如何过滤你检索的数据的问题。或者,在这种情况下,您如何不过滤它。
尝试这样的事情(适应你的需要)
在你的tableViewVC
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let vc = UIStoryboard(name: "Name", bundle: nil).instantiateViewController(withIdentifier: "vcIdentifier") as! YourViewController
vc.prepareVC(withId: indexPath.row)
self.navigationController?.pushViewController(vc, animated: true)
}
在你的guestPhotoViewController
class guestPhotoViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var backgroundProfileImage: UIImageView!
@IBOutlet weak var profileImage: roundImage!
@IBOutlet weak var fullNameLabel: UILabel!
@IBOutlet weak var postCell: UITableViewCell!
@IBOutlet weak var postTableView: UITableView!
private var ref: FIRDatabaseReference!
private var posts = [Post]()
private var posterId: Int = -1
override func viewDidLoad() {
super.viewDidLoad()
ref = FIRDatabase.database().reference()
loadUserPosts()
}
func prepareVC(withId: id) {
self.posterId = id
}
func loadUserPosts(){
if self.id != -1 {
if let ref = ref {
/* Probably something around here is messed up. You don't seem to be filtering by id, just retrieving "post".
You might want to consider checking how you filter (you don't seem to do so at all) the postRef, since that seems to be the issue.
I don't know your FireBase models, so it's harder to help you with that.
*/
let postsRef = ref.child("post").equalTo(self.posterId)
/*
or if they are ordered, you can use:
let postsRef = ref.queryOrderedByChild("post").queryEqualToValue(self.posterId)
*/
postsRef.observe(.value, with: { (snapshot) in
for post in snapshot.children {
let post = Post(snapshot: post as! FIRDataSnapshot)
self.posts.append(post)
print(post)
self.postTableView.reloadData()
}
})
}
}
}
}
关于swift - 如何检索从 TableView 中选择的用户的帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43745429/