未显示从Firebase接收数据的CollectionViewCell

时间:2019-05-17 08:24:34

标签: swift firebase firebase-realtime-database uicollectionview uicollectionviewcell

我必须用Firebase的数据填充单元格。但是它们不会显示。帮助并解释我在哪里出错。 如何填写单元格数据?

class TrainingProgramViewController: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView!
    //var trainingPrograms = TrainingProgram.fetchTrainingProgram()
    var trainingPrograms = [TrainingProgram]()
    let cellScale: CGFloat = 0.7


    override func viewDidLoad() {
        super.viewDidLoad()
        fetchPrograms()

    }

    func fetchPrograms() {
        Database.database().reference().child("programs").observe(.childAdded) { (snapshot) in
            if let dict = snapshot.value as? [String: Any] {
                print(dict)
                let newTitle = dict["title"] as! String
                print("Новый тайтл:" + newTitle)
                let newDescription = dict["description"] as! String
                let trainingCell = TrainingProgram(description: newDescription, title: newTitle)
                self.trainingPrograms.append(trainingCell)
                print(self.trainingPrograms)
            }
        }
    }


}

extension TrainingProgramViewController: UICollectionViewDataSource {

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

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TrainingProgramCollectionViewCell", for: indexPath) as!  TrainingProgramCollectionViewCell
        let trainingProgram = trainingPrograms[indexPath.item]
        cell.trainingPrograms = trainingProgram

        return cell

    }

这是一个模型:

class TrainingProgram
{
    var description = ""
    var title = ""


    init(description: String, title: String) {
        self.description = description
        self.title = title
    }
}

这是我的数据库结构:enter image description here

2 个答案:

答案 0 :(得分:0)

您忘记重新加载您的collectionView

func fetchPrograms() {
    Database.database().reference().child("programs").observe(.childAdded) { (snapshot) in
        if let dict = snapshot.value as? [String: Any] {
            print(dict)
            let newTitle = dict["title"] as! String
            print("Новый тайтл:" + newTitle)
            let newDescription = dict["description"] as! String
            let trainingCell = TrainingProgram(description: newDescription, title: newTitle)
            self.trainingPrograms.append(trainingCell)
            print(self.trainingPrograms)
        }
        DispatchQueue.main.async {
             self. collectionView.reloadData()
          }
    }
}

也设置

 collectionView.delegate = self
 collectionView.dataSource = self

在您的collectionView方法中

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TrainingProgramCollectionViewCell", for: indexPath) as!  TrainingProgramCollectionViewCell
    cell.titleLabel.text = trainingPrograms[indexPath.item].title

    return cell

}

答案 1 :(得分:0)

获取数据并将其附加到tableView / collectionView中显示的数组后,您始终必须像reloadData()collectionView.reloadData()那样tableView.reloadData()