Swift 提取数据时,UICollectionView不会更新

Swift 提取数据时,UICollectionView不会更新,swift,uicollectionview,uikit,Swift,Uicollectionview,Uikit,有人能帮忙吗?函数fetchUser()正在工作,我看到正在加载用户,但是集合视图的单元格没有出现。它也是通过编程实现的 集合视图为: let newCollection: UICollectionView = { let layout = UICollectionViewFlowLayout() let collection = UICollectionView(frame: CGRect(x: 0, y: 0, width: 0, height: 0), collection

有人能帮忙吗?函数
fetchUser()
正在工作,我看到正在加载用户,但是集合视图的单元格没有出现。它也是通过编程实现的

集合视图为:

let newCollection: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    let collection = UICollectionView(frame: CGRect(x: 0, y: 0, width: 0, height: 0), collectionViewLayout: layout)
    layout.scrollDirection = .horizontal
    collection.backgroundColor = UIColor.green
    collection.translatesAutoresizingMaskIntoConstraints = false
    collection.isScrollEnabled = true
    return collection
}()
配置的集合视图单元格包括:

extension WorkerUICollection: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return worker.count
    }

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! CustomeCell
        let cellUsers = worker[indexPath.item]
        cell.textLabel.text! = cellUsers.workerName
        //cell.workerLocation.text! = "2.0 km"
        cell.backgroundColor  = .white
        cell.layer.cornerRadius = 5
        cell.layer.shadowOpacity = 3
        //cell.imageView.image = UIImage(named: "user")
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 150, height: 250)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 3, left: 3, bottom: 3, right: 3)
    }

}
fetchUser()函数:

func fetchUser() {
    Database.database().reference().child("Users").observe(DataEventType.childAdded, with: { (snapshot) in
        if let dictionary = snapshot.value as? [String: AnyObject] {
            let worker = WorkerInfo()
            worker.workerName = (dictionary["username"] as! String?)!
            //user.profileImageURL = dictionary["profileImageURL"] as! String?
            DispatchQueue.main.async(execute: {
                self.newCollection.reloadData()
            })
        }
        print(snapshot)
    }, withCancel: nil)
}

有什么想法吗?

因为你的self.worker.count=0。在
fetchUser()
函数中,您应该使用数据库中的新数据源更新数据源(worker)。试试这样的

if let array = snapshot.value as? [String: AnyObject] {
   var tempWorkersArray: [WorkerInfo] = []

   for dict in array {
        let worker = WorkerInfo()
        worker.workerName = (dict["username"] as? String) ?? ""
        tempWorkersArray.append(worker)
   }
   self.worker = tempWorkersArray

   DispatchQueue.main.async {
       self.newCollection.reloadData()
   }
}

fetchUser()
是如何工作的?将此方法添加到您的帖子。我编辑了帖子
newCollection
的框架是
0,0,0
,如何/何时使其“可见”?你还有
worker.count
,所以我猜你有一个数组
worker
,如果是这样,
workers
会是一个更好的名字,但我不知道你从FireBase(
worker.append()
)的新值附加到哪里,谢谢!!!!现在可以了!我忘记了fetchUser()函数中的self.worker.append(worker)