I am trying to get data from my Firebase database into my UITableViewController
.
The meaning of the UITableViewController
is to display who has liked the post you are visiting.
When someone likes a post, their userID
is being placed in the post under the category likesForPost
- as you can see here:
As you can see 1 person has already liked this post (the black box is a 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. 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 :-)
Here is my 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:
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.
Try and do this
DispatchQueue.main.async {
self.updates = newUpdates
self.tableView.reloadData()
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.