ios - Xcode中UICollectionsView的单元格没有出现
问题描述
我正在开发一个应用程序并设置一个 UICollectionView。下面是 UICollectionView 所在的视图控制器的代码:
import UIKit
import Firebase
import FirebaseFirestoreSwift
import FirebaseFirestore
class scrollCollectionViewController: UICollectionViewController{
var tournaments = [String]()
@IBOutlet weak var collectionview: UICollectionView!
override func viewDidLoad() {
fetchTourneys()
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Register cell classes
// Do any additional setup after loading the view.
}
func fetchTourneys() {
let db = Firestore.firestore()
db.collection("Tournaments").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
print("\(document.documentID) => \(document.data())")
self.tournaments.append(document.documentID)
}
}
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
// MARK: UICollectionViewDataSource
override func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return self.tournaments.count
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return 5
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "tourneyIdentifier", for: indexPath) as! ScrollCollectionViewCell
cell.tournamentTitle.text = tournaments[indexPath.row]
print(cell.tournamentTitle.text)
// Configure the cell
return cell
}
// MARK: UICollectionViewDelegate
/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
override func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
return true
}
*/
/*
// Uncomment this method to specify if the specified item should be selected
override func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
return true
}
*/
/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
override func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool {
return false
}
override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
return false
}
override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {
}
*/
}
细胞只是没有最终出现。在包含一些打印语句后,我注意到 numberOfSections 的任何覆盖函数或集合视图似乎都没有运行。为什么这些没有运行,以及为什么细胞没有出现,可能是什么问题?
解决方案
请fetchTourneys()
在super.viewDidLoad() 之后移动。此外,您需要确保正确设置单元格标识符并在您的 collectionView 中注册
private let reuseIdentifier = "tourneyIdentifier"
class scrollCollectionViewController: UICollectionViewController {
var tournaments = [String]()
@IBOutlet weak var collectionview: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
// Register cell classes
self.collectionview!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
fetchTourneys()
}
然后,在创建单元格时,重新使用reuseIdentifier
.dequeueReusableCell(withReuseIdentifier: reuseIdentifier
此外,在您的 Firebase 函数中,确保在填充数据源后告诉 collectionView 进行更新
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
print("\(document.documentID) => \(document.data())")
self.tournaments.append(document.documentID)
}
self.collectionview.reloadData()
}
你还说
我注意到 numberOfSections 的任何覆盖函数或集合视图似乎都没有运行
这表明您的 UICollectionView 不知道此代码是 viewController。确保您已在 XCode Inspector 中进行设置。一般来说,Classes 和 Structs 应该以大写字母开头,vars 是小写的
推荐阅读
- kubernetes - 基于自定义标头的 Istio 流量路由
- javascript - Discord.js 如何访问 channel.members 中的信息?
- php - 查看与您的 MariaDB 服务器版本相对应的手册,了解在第 1 行的“LIMIT 1”附近使用的正确语法
- mongodb - 用于存储评论或消息的 mongodb 嵌入式 vs 参考
- flutter - Flutter 如何正确显示带有阴影的矩阵
- node.js - NodeJS 部署 Https
- twilio - 使用 Twilio 在客户端和机器人之间创建流式会议
- python - 是否有任何 JSON 标签过滤示例?
- arrays - Scala试图从数据框中的udt(数组)中提取值
- mongodb - 如何使用变量创建新的 Mongo ObjectId?