【发布时间】:2017-02-16 01:27:03
【问题描述】:
我目前有一个 UITableView 在单元格中显示 Firebase 数据库中的帖子。还有另一个 UITableView,我想只显示登录用户的帖子。
这是我目前在我的 UserPostViewController 中拥有的:
func loadData() {
let uid = FIRAuth.auth()?.currentUser?.uid
FIRDatabase.database().reference().child("posts").child(uid!).observeSingleEvent(of: .value, with: {
(snapshot) in
if let postsDictionary = snapshot.value as? [String: AnyObject] {
for post in postsDictionary {
self.userPosts.add(post.value)
}
self.userPostsTableView.reloadData()
}
})
}
// MARK: - Table view data source
func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// pass any object as parameter, i.e. the tapped row
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return self.userPosts.count
}
// Displays posts in postsTableView
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell", for: indexPath) as! PostTableViewCell
// Configure the cell...
let post = self.userPosts[indexPath.row] as! [String: AnyObject]
cell.titleLabel.text = post["title"] as? String
cell.priceLabel.text = post["price"] as? String
if let imageName = post["image"] as? String {
let imageRef = FIRStorage.storage().reference().child("images/\(imageName)")
imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in if error == nil {
let image = UIImage(data: data!)
cell.titleLabel.alpha = 0
cell.contentTextView.alpha = 0
cell.postImageView.alpha = 0
UIView.animate(withDuration: 0.4, animations: {
cell.titleLabel.alpha = 1
cell.contentTextView.alpha = 1
cell.postImageView.alpha = 1
})
} else {
print("Error occured during image download: \(error?.localizedDescription)")
}
})
}
return cell
}
这也是我在 Firebase 中的意思的图片:
【问题讨论】:
标签: ios swift uitableview firebase firebase-realtime-database