【问题标题】:Why is my tableview getting an error when im using this code firebase Swift 4当我使用此代码 firebase Swift 4 时,为什么我的 tableview 会出错
【发布时间】: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,但实际上它是一个 stringuserPosts 中是否有不是字典的项目?

标签: ios uitableview firebase firebase-realtime-database swift4


【解决方案1】:

试试这个以避免崩溃

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
}

【讨论】:

  • @Jiyar 查看我的答案
  • 现在试过了,但是没有用,它重复了两个帖子 3 次,所以总共有 6 个帖子,数据库中没有任何内容被检索
  • 至少我没有收到错误,但它变得更糟了哈哈
猜你喜欢
  • 1970-01-01
  • 2013-01-03
  • 2018-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多