I'm building an instagram clone and I managed to let users post a photo from their library and add a caption. The photo is then successfully displayed in a feed table view with the caption. However, I am unable to show the username of the user who posted the photo and his profile picture. There are no errors at all, I carefully followed a tutorial on Udemy writing the exact same code but it just won't show up.
//feedViewController
import UIKit
import Firebase
import SDWebImage
class FeedViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var posts = [Post]()
override func viewDidLoad() {
super.viewDidLoad()
loadPosts()
tableView.dataSource = self
tableView.estimatedRowHeight = 375
tableView.rowHeight = UITableView.automaticDimension
}
func loadPosts() {
Database.database().reference().child("posts").observe(.childAdded) { (snapshot: DataSnapshot) in
if let dict = snapshot.value as? [String: Any] {
let newPost = Post.transformPost(dict: dict)
self.posts.append(newPost)
self.tableView.reloadData()
}
}
}
}
extension FeedViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell", for: indexPath) as! HomeTableViewCell
let post = posts[indexPath.row]
cell.post = post
return cell
}
}
// HomeTableViewCell
import UIKit
import FirebaseDatabase
import SDWebImage
class HomeTableViewCell: UITableViewCell {
@IBOutlet weak var profileImageView: UIImageView!
@IBOutlet weak var usernameLabel: UILabel!
@IBOutlet weak var postImageView: UIImageView!
@IBOutlet weak var captionLabel: UILabel!
@IBOutlet weak var cheerButton: UIButton!
@IBOutlet weak var donateButton: UIButton!
@IBOutlet weak var commentButton: UIButton!
var post: Post? {
didSet {
updateView()
}
}
func updateView() {
captionLabel.text = post?.caption
if let photoUrlString = post?.photoUrl {
let photoUrl = URL(string: photoUrlString)
postImageView.sd_setImage(with: photoUrl)
}
setUpUserInfo()
}
func setUpUserInfo() {
if let uid = post?.uid {
Database.database().reference().child("users").child(uid).observeSingleEvent(of: DataEventType.value, with: {
snapshot in
if let dict = snapshot.value as? [String: Any] {
let user = User.transformUser(dict: dict)
self.usernameLabel.text = user.username
if let photoUrlString = user.profileImageUrl {
let photoUrl = URL(string: photoUrlString)
self.profileImageView.sd_setImage(with: photoUrl)
}
}
})
}
}
override func awakeFromNib() {
super.awakeFromNib()
usernameLabel.text = ""
captionLabel.text = ""
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
// Post (class)
import Foundation
class Post {
var caption: String?
var photoUrl: String?
var uid: String?
}
extension Post {
static func transformPost(dict: [String: Any]) -> Post {
let post = Post()
post.caption = dict["caption"] as? String
post.photoUrl = dict["photoUrl"] as? String
post.uid = dict["uid"] as? String
return post
}
}
// User (class)
import Foundation
class User {
var email: String?
var profileImageUrl: String?
var username: String?
}
extension User {
static func transformUser(dict: [String: Any]) -> User {
let user = User()
user.email = dict["email"] as? String
user.profileImageUrl = dict["profileImageUrl"] as? String
user.username = dict["username"] as? String
return user
}
}
答案 0 :(得分:0)
感谢您的答复!无论如何,我只是发现问题出在哪里。我将数据库子引用设置为“用户”,而数据库中引用的真实名称为“用户”。愚蠢的错字错误...
再次感谢大家