简体   繁体   中英

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

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

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

 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). 2017-11-23 14:17:30.616973+0100 Acty10[21308:164951] Could not cast value of type '__NSCFString' (0x1043fcf68) to 'NSDictionary' (0x1043fdf58). (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.

here is the cellForRowAt code

 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM