我在尝试检索之前从tableview中选择的用户的帖子时遇到了麻烦。我能够检索用户信息,如姓名,个人资料照片等,但我在检索他们的帖子时遇到了困难。目前我的代码没有返回任何内容。我在下面发布了我的代码。我遇到问题的地方在guestPhotoViewController
,我称之为函数loadUserPosts()
。任何帮助将非常感谢,提前感谢!
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!
}
答案 0 :(得分:0)
一些事情:
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()
}
})
}
}
}
}