Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.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
Swift 从VC1中的firebase检索用户数据并将其发送到VC2中要显示的TableView时出现问题_Swift_Firebase_Google Cloud Firestore_Tableview - Fatal编程技术网

Swift 从VC1中的firebase检索用户数据并将其发送到VC2中要显示的TableView时出现问题

Swift 从VC1中的firebase检索用户数据并将其发送到VC2中要显示的TableView时出现问题,swift,firebase,google-cloud-firestore,tableview,Swift,Firebase,Google Cloud Firestore,Tableview,我从firebase firestore检索所有数据时遇到问题(firebase firestore跟踪一些用户信息,即标题、说明和图像URL),然后从firebase Storage下载图像,创建一个名为Cella的对象(标题:字符串、说明:字符串、图像:UIImage)。这个过程应该在一个循环中进行,然后为每个用户创建一个对象,并返回一个Cella对象数组,该数组将传递给另一个ViewController并显示在其tableView上 我试图修复完成处理程序,因为我认为它们是问题所在,然后我

我从firebase firestore检索所有数据时遇到问题(firebase firestore跟踪一些用户信息,即标题、说明和图像URL),然后从firebase Storage下载图像,创建一个名为Cella的对象(标题:字符串、说明:字符串、图像:UIImage)。这个过程应该在一个循环中进行,然后为每个用户创建一个对象,并返回一个Cella对象数组,该数组将传递给另一个ViewController并显示在其tableView上

我试图修复完成处理程序,因为我认为它们是问题所在,然后我添加了一个IBAction,它在执行segue时被触发

我尝试检索数据的ViewController(请注意,它位于导航视图控制器内,并且segue是唯一一个,当按下该按钮时会触发)

表视图所在的ViewController:

class ViewControllerForTable: UIViewController, UITableViewDataSource, UITableViewDelegate {

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let currentCell = tableView.cellForRow(at: indexPath) as! TableViewCell

    let information = Information(title: currentCell.title.text!, description: currentCell.bodyText.text!, sellerImage: currentCell.CellImage.image!)

    let destinationVC = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerDisplay") as! ViewControllerDisplay

    destinationVC.dataPassed = information

    self.navigationController?.pushViewController(destinationVC, animated: true)
}


@IBOutlet weak var UITableView: UITableView!


let image : UIImage = UIImage(named: "image1")!

var cells : [Cella] = []

override func viewDidLoad() {
    super.viewDidLoad()

    //states that this class is the delegate of the data and the object tableView within this VC
    UITableView.delegate = self
    UITableView.dataSource = self

    //force sets each of the the tableView rows to have a height of 200
    UITableView.rowHeight = 200

    // Do any additional setup after loading the view.
}

//Function hardcoded that creates the 5 cells to test the app

//MARK - Table view settings

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    //meaning my UITableView is going to have cells.count different rows
    return cells.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    //Gets each item singularly in order from the dictionary of cells
    let cellFormat = cells[indexPath.row]

    //You need to do this Nib thing because you created a xib file

    let nib = UINib(nibName: String(describing: TableViewCell.self), bundle: nil)

    tableView.register(nib, forCellReuseIdentifier: "customCell")

    // Says that it is going to create a reusable cell with the identifier from the XIB file and it is going to use the class TableViewCell to access its properties
    let cellObject = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! TableViewCell

    //Creates and assigns the values from the item in the dictionary to the CellFormat for them to be displayed in the custom cell

    cellObject.setCell(cellFormatTaken: cellFormat)

    // returns the final Cell Item

    return cellObject

}

@IBAction func unwindToActivitieslList(sender: UIStoryboardSegue) {
    let sourceViewController = sender.source as? ViewController
    let activities = sourceViewController?.cellaObjects
    cells = activities!
    UITableView.reloadData()
}
}
------------------更新------------------ 我已经删除了

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)

    getDocumentsFromFirestore(firestoreReference: firestoreUsersReference) { (cellaArray) in
        self.cellaObjects = cellaArray
    }

}
并添加了按钮 @iAction func Sardinian已压缩(u发件人:ui按钮){


我还按照好心的建议添加了调度。尽管我无法获得要显示的数据。我对该按钮的另一个问题是,每当我单击它时,由于VCs嵌入到导航控制器中,它们会创建一个“副本”目标ViewController,并且它在应用程序打开的整个过程中一直这样做,给我留下了许多重复的、空的行。

您需要在
prepare(对于segue:
(直接在按钮操作中)之外调用它,并在完成后执行segue

getDocumentsFromFirestore(firestoreReference: firestoreUsersReference) { (cellaArray) in
    self.cellaObjects = cellaArray
    self.performSegue(withIdentifier:"segue",sender:nil)
}

这里还需要
DispatchGroup()
,因为获取每个项目的映像是异步的

func getDocumentsFromFirestore (firestoreReference: CollectionReference, completion: @escaping ([Cella])->()) {


    var cellaArray : [Cella] = []

    firestoreUsersReference.getDocuments { (querySnapshot, err) in
        if err != nil {
            print("There has been an error \(String(describing: err?.localizedDescription))")
        }
        else {

            let g = DispatchGroup()

            for documents in querySnapshot!.documents {

                g.enter()
                print("\(documents.documentID) => \(documents.data())")
                let data = documents.data()
                let title = data["userTitle"] as! String
                let description = data["userDescription"] as! String
                let imageURL = data["userImageURL"] as! String
                print("Title: \(String(describing: title)), Description: \(String(describing: description)), imageURL: \(imageURL)")

                self.cellCreationProcess(title: title, description: description, imageURL: imageURL, completion: { (newCell) in
                    cellaArray.append(newCell)
                    g.leave()
                })


            }

            g.notify(queue: .main, execute: {
                 completion(cellaArray)
            })

        }

    }
}

您需要在
prepare(对于segue:
(直接在按钮操作中)之外调用此函数,并在完成时执行segue

getDocumentsFromFirestore(firestoreReference: firestoreUsersReference) { (cellaArray) in
    self.cellaObjects = cellaArray
    self.performSegue(withIdentifier:"segue",sender:nil)
}

这里还需要
DispatchGroup()
,因为获取每个项目的映像是异步的

func getDocumentsFromFirestore (firestoreReference: CollectionReference, completion: @escaping ([Cella])->()) {


    var cellaArray : [Cella] = []

    firestoreUsersReference.getDocuments { (querySnapshot, err) in
        if err != nil {
            print("There has been an error \(String(describing: err?.localizedDescription))")
        }
        else {

            let g = DispatchGroup()

            for documents in querySnapshot!.documents {

                g.enter()
                print("\(documents.documentID) => \(documents.data())")
                let data = documents.data()
                let title = data["userTitle"] as! String
                let description = data["userDescription"] as! String
                let imageURL = data["userImageURL"] as! String
                print("Title: \(String(describing: title)), Description: \(String(describing: description)), imageURL: \(imageURL)")

                self.cellCreationProcess(title: title, description: description, imageURL: imageURL, completion: { (newCell) in
                    cellaArray.append(newCell)
                    g.leave()
                })


            }

            g.notify(queue: .main, execute: {
                 completion(cellaArray)
            })

        }

    }
}

我试着按照你善意的建议去做,不幸的是我没能得到结果。我已经用遇到的问题更新了帖子。将segue源代码挂到vc本身,而不是挂到按钮。我也试过这样做,但同样的事情也发生了。为了简单起见,我已经将它上传到github的以下存储库中:如果这可能的话lpI已经试着按照你善意的建议做了,不幸的是我没能得到结果。我已经用遇到的问题更新了帖子。将segue源代码挂到vc本身,而不是挂到按钮。我也尝试过这样做,但同样的事情也发生了。为了简单起见,我已经将它上传到github的以下存储库中:如果可能的话
xcode
标记说明中的帮助:“用法说明:此标记仅用于有关xcode IDE本身的问题,而不用于一般的Mac或iOS编程主题。”这里的代码太多。基本上,您必须创建操作并为图像获取操作添加依赖项。在所有操作完成后发出通知。您需要重新设计当前代码,因为完成处理程序可能在许多地方返回空。根据
xcode
标记的描述:用法说明:此标记仅用于有关Xcode IDE本身的问题,而不用于一般的Mac或iOS编程主题。“此处的代码太多。基本上,您必须创建操作并为图像获取操作添加依赖项。在所有操作完成后发出通知。您需要重新设计当前代码,因为完成处理程序可能在许多地方返回空。”。。