[英]Why do I keep receiving a “fatal error” when trying to fetch a UILabel for my UITableView?
I'm a beginner coder and I've hit a seeming impasse.我是一名初学者,我似乎陷入了僵局。 I'm setting up a UITableView
to display blog posts from users.我正在设置UITableView
来显示用户的博客文章。 I can successfully retrieve information from my Firestore database, but am having trouble actually populating this information into my UITableViewCell
.我可以成功地从我的 Firestore 数据库中检索信息,但是在将这些信息实际填充到我的UITableViewCell
中时遇到了麻烦。 I think this is down to the way my 'PostTableViewCell' is set up.我认为这取决于我的“PostTableViewCell”的设置方式。 Any answers to this would be immensely appreciated.对此的任何答案将不胜感激。 Here is what I have:这是我所拥有的:
Home View Controller:主页查看 Controller:
var postArray: [String] = []
var documents: [DocumentSnapshot] = []
let db = Firestore.firestore()
let currentUserID = Auth.auth().currentUser?.uid
// Find the UserIDs of people following
// Where Field for those UserIDs in "Posts"
override func viewDidLoad() {
super.viewDidLoad()
getFollowingPosts()
configureTableView()
}
func getFollowingPosts() {
let searchForFollowing = db.collection("users").document(currentUserID!).collection("Following")
searchForFollowing.getDocuments { (snapshot, error) in
for documents in snapshot!.documents {
let followedUID = documents.get("uid")
print(followedUID!)
self.db.collection("posts").whereField("uid", isEqualTo: followedUID!).getDocuments { (querySnapshot, error) in
for documents in querySnapshot!.documents {
let uid = documents.get("uid") as! String
let title = documents.get("Title") as! String
let ProfilePictureURL = documents.get("ProfilePictureURL") as! String
let username = documents.get("username") as! String
let content = documents.get("Content") as! String
self.postArray.append(title)
print(self.postArray)
}
self.tableView.reloadData()
}
}
}
}
func configureTableView() {
tableView.delegate = self
tableView.dataSource = self
tableView.register(PostTableViewCell.self, forCellReuseIdentifier: "PostCell")
// remove separators for empty cells
tableView.tableFooterView = UIView()
// remove separators from cells
tableView.separatorStyle = .none
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
postArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell", for: indexPath) as! PostTableViewCell
let post = postArray[indexPath.row]
**cell.titleLabel.text = post** //Error is encountered here
return cell
}
PostTableViewCell: PostTableViewCell:
class PostTableViewCell: UITableViewCell {
@IBOutlet weak var usernameLabel: UILabel!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var contentLabel: UILabel!
}
As I said in the title, I keep receiving a 'Fatal Error: unexpectedly found nil while implicitly unwrapping an Optional value" whenever I try to build, leading me to believe something is wrong with my 'PostTableViewCell' - this point is marked with double asterisks in my code. Any information and answers would be massively appreciated. I'm a real beginner, and eager to progress from this standstill.正如我在标题中所说,每当我尝试构建时,我都会不断收到“致命错误:在隐式展开可选值时意外发现 nil”,这让我相信我的“PostTableViewCell”有问题——这一点被标记为双我的代码中的星号。任何信息和答案都将不胜感激。我是一个真正的初学者,并渴望从这种停滞状态中取得进步。
tableView.register(PostTableViewCell.self, forCellReuseIdentifier: "PostCell")
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.