Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从firebase获取的图像在添加到tableViewCells的过程中是否重复?_Ios_Swift_Firebase_Firebase Realtime Database - Fatal编程技术网

Ios 从firebase获取的图像在添加到tableViewCells的过程中是否重复?

Ios 从firebase获取的图像在添加到tableViewCells的过程中是否重复?,ios,swift,firebase,firebase-realtime-database,Ios,Swift,Firebase,Firebase Realtime Database,以下代码从firebase获取图像,但错误地复制了两个图像。我认为这是由于我尝试过的所有位置都没有放置self.tableView.reloadData()。有人能给我一些建议吗 func fetchAllUsersImages() { print("inside func") self.ref.child("Posts").child(self.userID).child(self.postNum).observe(.childAdded, with: { snapsh

以下代码从firebase获取图像,但错误地复制了两个图像。我认为这是由于我尝试过的所有位置都没有放置
self.tableView.reloadData()
。有人能给我一些建议吗

    func fetchAllUsersImages() {
    print("inside func")
    self.ref.child("Posts").child(self.userID).child(self.postNum).observe(.childAdded, with: { snapshot in
        if let snapShotValue = snapshot.value as? [String: String] {
            for (_, value) in snapShotValue {
                if let imageURL = URL(string: value) {
                    print(imageURL, "image url here")
                    do {
                        let imageAsData = try Data(contentsOf: imageURL)
                        let image = UIImage(data: imageAsData)
                        let ImageObject = Image()
                        ImageObject.image = image
                        self.arrayOfImgObj.append(ImageObject)
                    } catch {
                        print("imageURL was not able to be converted into data")
                    }
                }
            }
        }
    })
}

确保在开始调用函数时清除了数组,因为您正在向数组添加数据。其次,在完成for循环后重新加载表

func fetchAllUsersImages() {
    self.arrayOfImgObj.removeAll() // clean the array

    self.ref.child("Posts").child(self.userID).child(self.postNum).observe(.childAdded, with: { snapshot in
        if let snapShotValue = snapshot.value as? [String: String] {
            for (_, value) in snapShotValue {

            }
            tableView.reloadData() // reload view
        }
    })
}

您可以在@Usama no找到一个解决方案,该解决方案不回答问题。在建议的解决方案中,他已从firebase检索到图像。@Usama我已经这样做了,问题是添加到单元格中的图像被复制了。我相信这是由于self.tableView.reloadData()的位置,它不在上面的代码中,因为我想知道它应该放在哪里才能避免这种情况,最初它是在eth do中block@TheGreatVisionary不要回复问题文本中的评论或其他答案,在你的问题或回答下面回复评论。如果人们看到该问题,他们会认为如果您(提问者)尚未单击其中一个答案旁边的复选标记以确认该答案为已接受答案,则该问题可能未得到回答。(请在有人提供有效答案时这样做)这仍然复制了图像。。。我在特定的位置有两个,它复制了这两个,在tableView中产生了4个图像