swift - UIcollection 视图中未显示数据。我做错了什么
问题描述
我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!
}
解决方案
您的numberOfItemsInSection
和cellForItemAt
函数正在查看posts
数组
但是在您的闭包中您将数据插入到song
变量中
重新加载集合时posts
仍然为空
推荐阅读
- java - org.apache.spark.sql.AnalysisException:找不到表和视图
- javascript - 谷歌表格脚本编辑器和正确解析 JSON
- machine-learning - 使用自动编码器的 1 的不兼容形状
- webrtc - 如何使用 OpenTok 选择音频输出
- javascript - 数据过滤器默认不显示任何内容
- angular - Clarity 带有指针的下拉菜单
- php - 谷歌日历 API 插入
- c# - 使 swagger.json 在 swashbuckle 中可编辑
- hadoop - Hadoop 服务启动后立即启动的纱线应用程序数量。集群是 4 个节点,即。Hadoop HA 集群
- python - 如何在python中提取文件数据