Ios Swift-Firebase获取所有钥匙并将其存储到阵列中

Ios Swift-Firebase获取所有钥匙并将其存储到阵列中,ios,swift,firebase,firebase-realtime-database,Ios,Swift,Firebase,Firebase Realtime Database,我有以下firebase数据库结构: Main ----- Name 1 --------value 1 --------value 2 --------value 3 ----- Name 2 --------value 1 --------value 1 --------value 1 我试图获取所有键值并将其存储到自定义类的数组中 我的自定义类有一个init: init(name: String, photo: String) { self._name = name self._phot

我有以下firebase数据库结构:

Main
----- Name 1
--------value 1
--------value 2
--------value 3
----- Name 2
--------value 1
--------value 1
--------value 1
我试图获取所有键值并将其存储到自定义类的数组中

我的自定义类有一个init:

init(name: String, photo: String) {
self._name = name
self._photo = photo
}
因此,在我的视图控制器中,我有:

var array = [customClass]()
let ref = Database.database().reference().child("Main")

func getInfo(){
    Ref.observeSingleEvent(of: .value) { (snapshot) in
        var arrayTemp = [customClass]()
        for child in snapshot.children {
            let snap = child as! DataSnapshot
            let name = snap.key
            let custom = customClass(Name: name, photo: "")
            arrayTemp.append(custom)
        }
        self.array = arrayTemp
        self.collectionView.reloadData()
    }
}

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as? Cell {
        let custom: Custom!
            custom = array[indexPath.row]
            cell.configureCell(custom)
              return cell
    } else {
    return UICollectionViewCell()
    }
}
这是牢房:

class Cell: UICollectionViewCell {

@IBOutlet weak var Thumb: UIImageView!
@IBOutlet weak var NameLbl: UILabel!

var custom: CustomClass!


func configureCell(_ custom: customClass) {

    self.custom = custom
    NameLbl.text = self.custom.customName.capitalized
    var url = URL(string: self.custom.photoUrl)
    if url == nil {
        url = URL(string: "")
    }
    Thumb.sd_setImage(with: url)

}
}

我的问题是,如果我打印名称,我拥有数据库中的所有值,但是当我重新加载集合视图时,我看到项目的数量是正确的,但是名称只是重复x次的第一个元素。。。 有什么帮助吗?

要解决您的问题:

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

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.array.count
}
要解决您的问题,请执行以下操作:

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

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

你能给我们看看你的cellForItemAtIndexPath函数吗?@Pipiks-thx用于回答。。。我已经在您的cellForItemAtIndexPath和configureCell中更新了问题,custom.customName是否正确,如果您是否打印它?@Pipiks否,它始终为我提供一个相同的值,并为正确的数组计数重复相同的值。所以现在我有3个项目,我看到3个名为“name 1”的单元格打印(custom.customName)在cellForItem中始终返回“name 1”?你能给我们看看你的cellForItemAtIndexPath函数吗?@Pipiks thx用于回答。。。我已经在您的cellForItemAtIndexPath和configureCell中更新了问题,custom.customName是否正确,如果您是否打印它?@Pipiks否,它始终为我提供一个相同的值,并为正确的数组计数重复相同的值。所以现在我有3个项目,我看到3个名为“name 1”的单元格打印(custom.customName)在cellForItem中始终返回“name 1”?