簡體   English   中英

當我使用此代碼firebase Swift 4時,為什么我的tableview出現錯誤

[英]Why is my tableview getting an error when im using this code firebase Swift 4

嘿,我有2個tableviews一個在mainpage於一個userprofilepage上的一個mainpage是表明存在,但是,在一個公共的所有帖子profilepage是相同的,但我想改變它,以便只有該用戶是當前該用戶uid的帖子在線顯示在profilepage tableview中

我當前正在使用此代碼,而該代碼僅顯示我的每條帖子,而不是僅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()
                }
            }
        }
}

但是然后我將其更改為此代碼,因為我將帖子也保存到用戶的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()
                }
            }
        }
}

我在控制台中收到此錯誤

無法將類型'__NSCFString'(0x1043fcf68)的值強制轉換為'NSDictionary'(0x1043fdf58)。 2017-11-23 14:17:30.616973 + 0100 Acty10 [21308:164951]無法將類型'__NSCFString'(0x1043fcf68)的值強制轉換為'NSDictionary'(0x1043fdf58)。 (lldb)

我很確定這段代碼應該可以正常工作,但是也許我做錯了

這是我的數據庫的圖像

在此處輸入圖片說明

因為您可以使用userpost內的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
}

謝謝你的時間 :)

試試這個避免崩潰

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM