关键词:
【中文标题】Xcode中集合视图的单元格未出现【英文标题】:Cells of UICollectionsView in Xcode Not Appearing 【发布时间】:2020-07-05 09:32:08 【问题描述】:我正在开发一个应用程序并设置一个 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 的覆盖函数或集合视图似乎都没有运行。为什么这些没有运行,以及为什么单元格没有出现,可能是什么问题?
【问题讨论】:
【参考方案1】:你需要在numberOfItemsInSection
中返回self.tournaments.count
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)
self.collectionview.reloadData()
override func numberOfSections(in collectionView: UICollectionView) -> Int
// #warning Incomplete implementation, return the number of sections
return 1
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
// #warning Incomplete implementation, return the number of items
return self.tournaments.count
【讨论】:
我刚刚修复了这个问题,但单元格仍然没有出现在模拟器中【参考方案2】:请移动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 是小写的
【讨论】:
感谢您的回答,我想我正在取得进展。但是,我现在收到错误消息,我的自定义单元格的插座都为零。当我删除集合视图寄存器行时,它们不再为零,但仍然不会加载到屏幕上。可能是什么错误? @NikhilChandra 该行是通用的,因为它将标准 UICollectionViewCell 注册到集合视图。你有一个自定义的 UICollectionViewCell 子类,所以注册你的子类。【参考方案3】:一旦fetchTourneys
完成,您必须在collectionview
上致电reloadData
。
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)
self.collectionview.reloadData()
【讨论】:
它似乎仍然不起作用。单元格不显示 我打印了单元格,现在显示它有一个值,但单元格仍然没有显示在模拟器中 你不需要在主线程上调用 reload ... 因为它已经在主线程上... 在 Firebase 闭包中不需要DispatchQueue.main.async
。 Firebase 闭包中的 UI 调用始终称为主线程。网络是在后台线程上完成的。【参考方案4】:
你需要在viewDidLoad中设置collectionview数据源并委托给self
将delegate = self
和dataSource = self
放入viewDidLoad
【讨论】:
【参考方案5】:每个人的答案都指出了代码中的错误,从而将其移向了正确的方向。但它仍然没有显示细胞。我打印了每个单元格,并注意到有一个参数使它们全部隐藏。我不知道是什么原因造成的。但我添加了以下代码:
cell.isHidden = false
效果很好!
【讨论】:
表格视图单元格未出现
】表格视图单元格未出现【英文标题】:tableviewcellnotappearing【发布时间】:2016-03-1704:17:06【问题描述】:我创建了一个自定义单元格视图,并在故事板中创建了一个默认图像。但由于某种原因,我的单元格没有出现。我应该怎... 查看详情
swift中集合视图单元格内的按钮操作
】swift中集合视图单元格内的按钮操作【英文标题】:Buttonactioninsideacollectionviewcellinswift【发布时间】:2021-01-2815:04:06【问题描述】:在我的集合视图中,有6个单元格。每个单元格都有一个按钮。如果我将点击任何按钮,则按钮... 查看详情
UI 测试中集合视图的错误单元格计数
】UI测试中集合视图的错误单元格计数【英文标题】:WrongcellscountforcollectionviewinUITests【发布时间】:2015-11-1219:36:03【问题描述】:我有一个像这样工作的集合视图的测试:functestDeleteItem()app.collectionViews.staticTexts["Item"].tap()app.butto... 查看详情
集合视图单元格未出现
】集合视图单元格未出现【英文标题】:CollectionViewCellsnotappearing【发布时间】:2016-02-2604:29:51【问题描述】:我想显示尽可能多的collectionViewCells和buttons,因为我的数组中有字符串。但是当我启动模拟器时,只有CollectionView的背... 查看详情
ios中集合视图中的不同单元格大小
】ios中集合视图中的不同单元格大小【英文标题】:DifferentCellsizeinacollectionviewinios【发布时间】:2014-11-0713:32:15【问题描述】:我已经使用UICollectionView将图像显示为网格视图,大小相同,如下所示下一步我想将单元格显示为不... 查看详情
处理 UICollectionViewCell 中的滚动视图项目(单元格未出现)
】处理UICollectionViewCell中的滚动视图项目(单元格未出现)【英文标题】:HandlescrollviewitemsinUICollectionViewCell(thecellisnotappearing)【发布时间】:2020-05-0914:44:01【问题描述】:我正在制作一个水平的UICollectionView,在UICollectionViewCell内... 查看详情
自定义单元格未出现在表格视图中
】自定义单元格未出现在表格视图中【英文标题】:customcellnotappearintableview【发布时间】:2013-02-1407:22:39【问题描述】:我使用故事板。我在情节提要中为我的Cell创建了新类,我用ReuseIdentifier“userFullCell”编写。我的Cell类.h#impo... 查看详情
我的自定义单元格未显示在我的表格视图中
】我的自定义单元格未显示在我的表格视图中【英文标题】:Mycustomcellsarenotshowingupinmytableview【发布时间】:2019-06-1702:59:12【问题描述】:所以我一直试图让我的自定义单元格显示在这个tableview上,但我不确定它们为什么没有显... 查看详情
表格视图中的自定义单元格未显示?
】表格视图中的自定义单元格未显示?【英文标题】:CustomCellintableviewnotshowing?【发布时间】:2020-03-2106:40:51【问题描述】:我无法让我的自定义单元格显示在我的表格视图中。我确保在情节提要中设置单元重用标识符并将我的I... 查看详情
取消隐藏部分堆栈视图时,带有堆栈视图的单元格未扩展
】取消隐藏部分堆栈视图时,带有堆栈视图的单元格未扩展【英文标题】:Cellwithstackviewnotexpandingwhenunhidingpartofstackview【发布时间】:2017-01-2014:08:21【问题描述】:我有一个带有tableview的viewcontroller,它的rowHeight属性设置为UITableV... 查看详情
选择的 UITableView 单元格未加载另一个视图
】选择的UITableView单元格未加载另一个视图【英文标题】:UITableViewcellselectednotloadinganotherView【发布时间】:2010-12-2100:29:23【问题描述】:大家好,我遇到了一个奇怪的问题。我试图让我的表格推送另一个视图,称为“productviewcon... 查看详情
单元格未在表格视图中正确显示
】单元格未在表格视图中正确显示【英文标题】:Celldoesn\'tshowupproperlyintableview【发布时间】:2017-07-1806:35:36【问题描述】:我正在使用TableView在ViewController上实现它。我面临的问题是细胞数量。目前单元格应该返回2行,但它只... 查看详情
表格视图单元格未正确更新
】表格视图单元格未正确更新【英文标题】:tableviewcellsnotupdatingproperly【发布时间】:2017-08-0915:32:22【问题描述】:我正在使用表格视图来显示下载的歌曲。我在下载歌曲之前创建空文件夹,下载歌曲后没有灭绝“.mp3”我正在... 查看详情
表视图行中集合视图的同步水平滚动
】表视图行中集合视图的同步水平滚动【英文标题】:Synchronizedhorizontalscrollingofcollectionviewsintableviewrows【发布时间】:2014-09-2221:27:25【问题描述】:我正在尝试在表格视图中同步所有集合视图中的滚动(请参见下面的图片链接)... 查看详情
自定义表格单元格未调整大小以适合内容视图
】自定义表格单元格未调整大小以适合内容视图【英文标题】:Customtablecellnotresizingtofitcontentview【发布时间】:2017-01-0805:50:51【问题描述】:我正在实现一个包含3种自定义单元格的表格视图。如下所示,在公司信息部分,我的... 查看详情
自定义表格视图单元格未根据笔尖调整大小
】自定义表格视图单元格未根据笔尖调整大小【英文标题】:CustomTableviewcellnotresizingaspernib【发布时间】:2011-10-3108:32:24【问题描述】:我通过继承UITableViewCell创建了一个新的自定义TableViewCell。我使用笔尖创建了一个带有UIImage... 查看详情
iOS - 带有 UITextView 的表格视图单元格未正确调整大小
】iOS-带有UITextView的表格视图单元格未正确调整大小【英文标题】:iOS-TableViewCellwithUITextViewnotresizingproperly【发布时间】:2013-02-0309:50:09【问题描述】:我已经对一个单元类进行了子类化,并且正在使用Storyboard中的原型。有问题... 查看详情
表格视图单元格未显示在情节提要中
】表格视图单元格未显示在情节提要中【英文标题】:TableViewCellnotdisplayinginStoryboard【发布时间】:2015-01-2620:26:59【问题描述】:我刚开始学习IOS编程。如果这个问题太入门,请提前道歉。使用故事板拖放,我简单地创建了一个... 查看详情