[英]Why is my tableview getting an error when im using this code firebase Swift 4
hey i have 2 tableviews one on the mainpage
one on userprofilepage
the one on the mainpage
is to show the public all the posts that exist but the one on the profilepage
is the same but i want to change it to so only that user that is currently online on that user uid's posts is shown in the profilepage
tableview 嘿,我有2个tableviews一个在
mainpage
于一个userprofilepage
上的一个mainpage
是表明存在,但是,在一个公共的所有帖子profilepage
是相同的,但我想改变它,以便只有该用户是当前该用户uid的帖子在线显示在profilepage
tableview中
i am currently using this code and it is this code that shows me every single post instead of only the ones that only ThatUser
has uploaded but this code does not give me error´s 我当前正在使用此代码,而该代码仅显示我的每条帖子,而不是仅
ThatUser
上传的ThatUser
,但此代码不会给我错误提示
func loadTableViewData(){
if ((Auth.auth().currentUser?.uid) != nil) {
Database.database().reference().child("userposts").observeSingleEvent(of: .value) { (snapshot) in
if let postsDictionary = snapshot.value as? [String: AnyObject] {
for userPost in postsDictionary {
self.userPosts.add(userPost.value)
}
self.userPostsTableView.reloadData()
}
}
}
}
but then i change it to this code because i'm saving the post to the user's uid aswell as shown in the database image but this one does not give me error but i when i open the app and the go to the userprofilepage
the app crashes 但是然后我将其更改为此代码,因为我将帖子也保存到用户的uid中,如数据库图像中所示,但这并没有给我错误,但是当我打开应用程序并转到
userprofilepage
,应用程序崩溃了
if let uid = Auth.auth().currentUser?.uid {
Database.database().reference().child("userposts").child(uid).observeSingleEvent(of: .value) { (snapshot) in
if let postsDictionary = snapshot.value as? [String: AnyObject] {
for userPost in postsDictionary {
self.userPosts.add(userPost.value)
}
self.userPostsTableView.reloadData()
}
}
}
}
and i get this error in the console 我在控制台中收到此错误
Could not cast value of type '__NSCFString' (0x1043fcf68) to 'NSDictionary' (0x1043fdf58).
无法将类型'__NSCFString'(0x1043fcf68)的值强制转换为'NSDictionary'(0x1043fdf58)。 2017-11-23 14:17:30.616973+0100 Acty10[21308:164951] Could not cast value of type '__NSCFString' (0x1043fcf68) to 'NSDictionary' (0x1043fdf58).
2017-11-23 14:17:30.616973 + 0100 Acty10 [21308:164951]无法将类型'__NSCFString'(0x1043fcf68)的值强制转换为'NSDictionary'(0x1043fdf58)。 (lldb)
(lldb)
iam pretty sure this code should generaly work but maybe i'm doing something wrong 我很确定这段代码应该可以正常工作,但是也许我做错了
Here is a image of my database 这是我的数据库的图像
as you can se iam using the uid inside of userposts then iam trying to retrive it the same way but i get that crash please help me someone. 因为您可以使用userpost内的uid来查找iam,然后iam尝试以相同的方式进行检索,但是我崩溃了,请帮助我。
here is the cellForRowAt
code 这是
cellForRowAt
代码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell", for: indexPath) as! UserProfileTableViewCell
// Configure the cell...
let userPost = self.userPosts[indexPath.row] as! [String: AnyObject]
cell.profileTitleLabel.text = userPost["title"] as? String
cell.profileContentView.text = userPost["content"] as? String
cell.profileTimeAndDateLabel.text = userPost["time"] as? String
cell.profileUsernameLabel.text = userPost["username"] as? String
cell.profileLocationAddress.text = userPost["adress"] as? String
if let imageName = userPost["image"] as? String {
let imageRef = Storage.storage().reference().child("images/\(imageName)")
imageRef.getData(maxSize: 25 * 1024 * 1024) { (data, error) -> Void in
if error == nil {
//successfull
let downloadedImage = UIImage(data: data!)
cell.profileImageView.image = downloadedImage
}else {
// error
print("there was an error downloading image: \(String(describing: error?.localizedDescription))")
}
}
}
return cell
}
thanks for your time :) 谢谢你的时间 :)
Try this to avoid crash 试试这个避免崩溃
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell", for: indexPath) as! UserProfileTableViewCell
// Configure the cell...
if let userPost = self.userPosts[indexPath.row] as? [String: AnyObject] {
cell.profileTitleLabel.text = userPost["title"] as? String
cell.profileContentView.text = userPost["content"] as? String
cell.profileTimeAndDateLabel.text = userPost["time"] as? String
cell.profileUsernameLabel.text = userPost["username"] as? String
cell.profileLocationAddress.text = userPost["adress"] as? String
if let imageName = userPost["image"] as? String {
let imageRef = Storage.storage().reference().child("images/\(imageName)")
imageRef.getData(maxSize: 25 * 1024 * 1024) { (data, error) -> Void in
if error == nil {
//successfull
let downloadedImage = UIImage(data: data!)
cell.profileImageView.image = downloadedImage
}else {
// error
print("there was an error downloading image: \(String(describing: error?.localizedDescription))")
}
}
}
}
return cell
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.