【问题标题】:Data not showing in the UIcollection view. What did I do wrong [closed]UIcollectionview 中未显示数据。我做错了什么[关闭]
【发布时间】:2021-04-03 01:25:30
【问题描述】:

我的UICollectionView 没有显示数据,我做错了什么?

这里是集合视图控制器类:

class SongfeedViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {

    @IBOutlet weak var collectionview: UICollectionView!
    
    var posts = [Post]()
    
    private var song = [songs]()
    private var songsCollection : CollectionReference!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        songsCollection = Firestore.firestore().collection("Songs")
    }
    
    override func viewWillAppear(_ animated: Bool) 
    {
        songsCollection.getDocuments { (snapshot, error) in
            if let err = error{
                print("Error Fetching Docs:\(err)")
            
            } else {
                guard let snap = snapshot else{return}
                for document in snap.documents{
                    let data = document.data()
                    let fullname = data["fullname"] as? String ?? "Anonymous"
                    let songname = data["songname"] as? String ?? "Anonymous"
                    let youtubelink = data["youtubelink"] as? String ?? "Anonymous"
                    let moneyoffer = data["moneyoffer"] as? String ?? "Anonymous"
                    let instalink = data["instalink"] as? String ?? "Anonymous"
                    
                    let newsong = songs(fullname: fullname, songname: songname, moneyoffer: moneyoffer, youtubelink: youtubelink, instalink: instalink)
                    
                    self.song.append(newsong)
                }

                self.collectionview.reloadData()
            }
        }
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.posts.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionview.dequeueReusableCell(withReuseIdentifier: "postCell", for: indexPath) as! postCell
        
        cell.artistnamelabel.text = self.posts[indexPath.row].fullname
        cell.moneyofferlabel.text = self.posts[indexPath.row].moneyoffer
        cell.instaUsernnamelabel.text = self.posts[indexPath.row].instalink
        cell.songnamelabel.text = self.posts[indexPath.row].songname
        cell.youtubelinklabel.text = self.posts[indexPath.row].youtubelink
        
        return cell
    }

来自 firestore 的数据未显示在 UI 集合视图上。

我的邮箱中的代码如下:

import UIKit

class postCell: UICollectionViewCell {
    @IBOutlet weak var artistnamelabel: UITextField!
    @IBOutlet weak var moneyofferlabel: UITextField!
    @IBOutlet weak var songnamelabel: UITextField!
    
    @IBOutlet weak var youtubelinklabel: UITextField!
    @IBOutlet weak var instaUsernnamelabel: UITextField! 
}

【问题讨论】:

  • 答案中提到了这一点,但这更像是一个错字。在委托函数collectionView:numberOfItemsInSection 中,此代码return self.posts.count 将始终返回0,因为该数组中没有添加任何内容。例如self.posts 已初始化 var posts = [Post]() 但从未填充数据。

标签: swift xcode firebase google-cloud-firestore storyboard


【解决方案1】:

尝试将您的SongfeedViewController 更改为:

class SongfeedViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

    @IBOutlet weak var collectionview: UICollectionView!
    
    var posts = [Post]()
    
    private var song = [songs]() {
        didSet {
            self.collectionview.reloadData()
        }
    }
    private var songsCollection : CollectionReference!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        songsCollection = Firestore.firestore().collection("Songs")
    }
    
    override func viewWillAppear(_ animated: Bool) 
    {
        songsCollection.getDocuments { (snapshot, error) in
            if let err = error {
                print("Error Fetching Docs:\(err)")
            } else {
                guard let snap = snapshot else { return }

                var songList = [songs]()

                for document in snap.documents 
                {
                    let data = document.data()
                    let fullname = data["fullname"] as? String ?? "Anonymous"
                    let songname = data["songname"] as? String ?? "Anonymous"
                    let youtubelink = data["youtubelink"] as? String ?? "Anonymous"
                    let moneyoffer = data["moneyoffer"] as? String ?? "Anonymous"
                    let instalink = data["instalink"] as? String ?? "Anonymous"
                    
                    let newsong = songs(
                        fullname: fullname, 
                        songname: songname, 
                        moneyoffer: moneyoffer, 
                        youtubelink: youtubelink, 
                        instalink: instalink
                    )
                    
                    songList.append(newsong)
                }

                self.song = songList
            }
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.song.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionview.dequeueReusableCell(withReuseIdentifier: "postCell", for: indexPath) as! postCell
        let song = self.song[indexPath.row]

        cell.artistnamelabel.text = song.fullname
        cell.moneyofferlabel.text = song.moneyoffer
        cell.instaUsernnamelabel.text = song.instalink
        cell.songnamelabel.text = song.songname
        cell.youtubelinklabel.text = song.youtubelink
        
        return cell
    }

    // -- Add this delegate method --
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        // Change this values as you want
        let numberOfItemsPerRow: CGFloat = 2 
        let spacingBetweenCells: CGFloat = 12
        let cellHeight: CGFloat = 200
        
        let totalSpacing = (2 * spacingBetweenCells) + ((numberOfItemsPerRow - 1) * spacingBetweenCells)
        
        let width = (collectionView.bounds.width - totalSpacing) / numberOfItemsPerRow
        
        return CGSize(width: width, height: cellHeight)
    }

【讨论】:

  • 试过了。没有工作仍然没有显示在“UICollectionView”上
  • 1.你能控制你是否正确设置了UICollectionView 约束? 2. 你能检查你的代码是否确实将newsongs 项添加到列表中?可能存在 JSON 解码错误。
【解决方案2】:

您的 numberOfItemsInSectioncellForItemAt 函数正在查看 posts 数组

但在您的闭包中,您将数据插入到song 变量中

当重新加载集合时,posts 仍然为空

【讨论】:

  • 尝试将所有内容更改为歌曲变量。还是行不通。你认为我上传到 Firestore 可能有问题吗?
  • 你添加了songsCollection.dataSource = self enywhere吗?
  • 不,我没有,但我确实为歌曲变量添加了一个 .swift 文件。
猜你喜欢
  • 1970-01-01
  • 2012-11-07
  • 2013-02-08
  • 1970-01-01
  • 1970-01-01
  • 2014-02-16
  • 2018-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多