【发布时间】:2017-11-23 13:27:43
【问题描述】:
嘿,我有 2 个表格视图,一个在 mainpage 上,一个在 userprofilepage 上,一个在 mainpage 上,是为了向公众展示所有存在的帖子,但 profilepage 上的那个是相同的,但我想要将其更改为仅在该用户 uid 的帖子上当前在线的用户显示在 profilepage tableview 中
我目前正在使用这段代码,正是这段代码向我展示了每一个帖子,而不是只有 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()
}
}
}
}
但是我将其更改为此代码,因为我将帖子保存到用户的 uid 以及数据库图像中显示的但这个并没有给我错误但是当我打开应用程序并转到 @ 987654330@应用崩溃
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()
}
}
}
}
我在控制台中收到此错误
无法将“__NSCFString”(0x1043fcf68) 类型的值转换为“NSDictionary”(0x1043fdf58)。 2017-11-23 14:17:30.616973+0100 Acty10 [21308:164951] 无法将“__NSCFString”(0x1043fcf68) 类型的值转换为“NSDictionary”(0x1043fdf58)。 (lldb)
我很确定这段代码应该可以正常工作,但也许我做错了什么
这是我的数据库的图像
因为您可以使用用户帖子中的 uid 来查看 iam,然后 iam 尝试以相同的方式检索它,但我遇到了崩溃,请帮助我。
这里是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
}
感谢您的宝贵时间:)
【问题讨论】:
-
为行代码添加单元格
-
这是@jigneshVadadoriya
-
在这一行发生崩溃??让 userPost = self.userPosts[indexPath.row] 为! [字符串:AnyObject]
-
发生了哪条线路崩溃?
-
错误信息表明您正在将一个对象转换为 dictionary,但实际上它是一个 string。
userPosts中是否有不是字典的项目?
标签: ios uitableview firebase firebase-realtime-database swift4