【发布时间】:2016-10-27 14:33:26
【问题描述】:
我正在尝试从我的 Firebase 数据库中获取数据到我的 UITableViewController。
UITableViewController 的含义是显示谁喜欢你正在访问的帖子。
当有人喜欢某个帖子时,他们的userID 将被放置在likesForPost 类别下的帖子中 - 正如您在此处看到的:
如您所见,已有 1 人点赞此帖子(黑框为 userID)。
问题是我的单元格上没有显示任何数据。我正在尝试在我的手机上显示userID,但没有显示任何内容。但是,我确实得到的单元格数量等于喜欢该帖子的人数。
我应该解释一下 viaSegue 正在从 prepareForSegue 获取密钥,是的,它正在显示正确的密钥,所以这不是问题 :-)
这是我的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
}
}
您可能还需要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]
}
}
【问题讨论】:
-
您在 snapshot.children 中收到什么了吗?你收到 self.updates 数组中的数据了吗?
-
@AndrewVeresov 我添加了一个
print以查看updateObject中的内容,我得到了这个:LikeSweet(key: "userID", userID: "", itemRef: ****link***, path: "userID") -
看起来问题出在
LikeSweet?当我使用此代码时:if let theFeedContent = snapshot.value!["userID"] as? String { userID = theFeedContent } else { userID = "Got nothing" }单元格标签写着“一无所有”.. 我只是不知道如何让它获取快照? -
所以在 cellForRow 中,您尝试设置 cell.nameLabel.text = update.userID,但在 ypur 响应中,您在 userID 字段中收到了空字符串。
-
检查快照。孩子们是否收到了空的用户 ID?
标签: ios swift uitableview firebase firebase-realtime-database