提取数据后,UICollectionView不会更新

时间:2018-09-23 19:45:22

标签: swift uicollectionview uikit

有人可以帮忙吗? 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)
}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

因为您的self.worker.count =0。在您的fetchUser()函数中,您应该更新数据库中的新数据源(工作人员)。尝试这样的事情。

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()
   }
}