[英]Get data from firebase into UITableViewController - swift
I am trying to get data from my Firebase database into my UITableViewController
. 我正在尝试将数据从Firebase数据库获取到
UITableViewController
。
The meaning of the UITableViewController
is to display who has liked the post you are visiting. UITableViewController
的含义是显示谁喜欢您正在访问的帖子。
When someone likes a post, their userID
is being placed in the post under the category likesForPost
- as you can see here: 当某人喜欢某条帖子时,他们的
userID
likesForPost
被放置在该帖子的“ likesForPost
”类别likesForPost
-您可以在此处看到:
As you can see 1 person has already liked this post (the black box is a userID
). 如您所见,已有1个人喜欢此帖子(黑框是
userID
)。
The problem is that I do not get any data displayed on my cell. 问题是我的单元格上没有显示任何数据。 I am trying to display the
userID
on my cell but nothing shows. 我正在尝试在我的单元格上显示
userID
,但是什么也没显示。 I do however get the number of cells equal to the number of people that has liked the post. 但是,我确实获得了与喜欢该职位的人数相等的单元数。
I should explain the that viaSegue
is getting a key from a prepareForSegue
and yes it is displaying the correct key, so that is not the problem :-) 我应该解释一下
viaSegue
从prepareForSegue
获取了一个密钥,是的,它显示了正确的密钥,所以这不是问题:-)
Here is my UITableViewController
: 这是我的
UITableViewController
:
import UIKit
import FirebaseDatabase
import FirebaseAuth
import FBSDKCoreKit
import FirebaseStorage
class viewLikesTableViewController: UITableViewController {
var viaSegue = "Nothing"
var updates = [LikeSweet]()
override func viewDidLoad() {
super.viewDidLoad()
let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView
print(viaSegue)
startObersvingDB()
}
func startObersvingDB() {
FIRDatabase.database().reference().child("feed-items").child(self.viaSegue).child("likesForPost").observeEventType(.Value, withBlock: { (snapshot: FIRDataSnapshot) in
// Get user value
var newUpdates = [LikeSweet]()
for update in snapshot.children {
let updateObject = LikeSweet(snapshot: update as! FIRDataSnapshot)
newUpdates.append(updateObject)
}
self.updates = newUpdates
self.tableView.reloadData()
}) { (error: NSError) in
print(error.description)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return updates.count
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let update = updates[indexPath.row]
let allDataSend = update.userID
self.performSegueWithIdentifier("showProfile", sender: allDataSend)
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:viewLikesTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! viewLikesTableViewCell
let update = updates[indexPath.row]
cell.nameLabel.text = update.userID
return cell
}
}
You might also want the LikeSweet
code: 您可能还需要
LikeSweet
代码:
import Foundation
import FirebaseDatabase
import FirebaseAuth
import UIKit
struct LikeSweet {
let key: String!
let userID: String!
let itemRef: FIRDatabaseReference?
let path : String!
init (userID: String, key: String = "") {
self.key = key
self.userID = userID
self.itemRef = nil
// self.path = dataPath
self.path = ""
}
init (snapshot: FIRDataSnapshot) {
key = snapshot.key
itemRef = snapshot.ref
path = key
if let theFeedContent = snapshot.value!["userID"] as? String {
userID = theFeedContent
} else {
userID = ""
}
}
func toAnyObject() -> AnyObject {
return ["userID":userID]
}
}
It should be as simple as calling your 它应该像打电话给您一样简单
self.updates = newUpdates
self.tableView.reloadData()
on the main queue. 在主队列上。 You can't update the UI on a non-main thread.
您不能在非主线程上更新UI。
Try and do this 尝试做这个
DispatchQueue.main.async {
self.updates = newUpdates
self.tableView.reloadData()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.