基于Firebase的iPhone应用:CollectionViewControllerCell无法显示求助
Hey Cody, let's work through why your CollectionViewController isn't even showing basic colored squares—especially since your Firebase data loads perfectly in the TableViewController. Let's break down the most common fixes step by step:
Start with the simplest checks, since these are usually the culprit when a collection view shows nothing:
Verify Data Source/Delegate Connections
Make sure your CollectionViewController is set as the collection view's data source and delegate. Either:- In Storyboard, control-drag from the collection view to your view controller and select both
dataSourceanddelegate. - Or add these lines in
viewDidLoad():collectionView.dataSource = self collectionView.delegate = self
Also double-check that your class conforms to the required protocols:
class YourCollectionVC: UICollectionViewController, UICollectionViewDelegateFlowLayout { // ... your code ... }- In Storyboard, control-drag from the collection view to your view controller and select both
Ensure Core Data Source Methods Are Implemented Correctly
Even if you plan to use Firebase data later, hardcode values first to test if the collection view works at all:override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { // Return a hardcoded number (like 5) instead of your Firebase data count first return 5 } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YourCellReuseID", for: indexPath) // Set a solid background color to test visibility cell.backgroundColor = .systemOrange return cell }Don't forget to set a cell size (if using Flow Layout)—without this, cells might have 0 width/height and be invisible:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: 100, height: 100) // Adjust as needed }
Since your TableViewController works, the issue is likely how you're handling data in the CollectionVC:
Confirm Data Is Actually Loading
Add a print statement right after you fetch data from Firebase to make sure the array isn't empty:// Inside your Firebase completion handler fetchedDataArray = snapshot.documents.map { /* your mapping logic */ } print("Fetched \(fetchedDataArray.count) items") collectionView.reloadData() // Critical: Don't forget to refresh the collection view!A common mistake is forgetting to call
collectionView.reloadData()after the data finishes loading—just like you do withtableView.reloadData()in the TableVC.Match the TableVC's Firebase Logic
Compare your CollectionVC's data fetch code line-by-line with the working TableVC code. Are you using the same collection reference? Is the mapping logic identical? Sometimes a small typo (like a wrong document field name) can leave your data array empty.
- Add breakpoints in
numberOfItemsInSectionandcellForItemAt—if they never get hit, your data source/delegate isn't connected properly. - Check the collection view's constraints in Storyboard: Make sure it's pinned to all edges of the view controller's main view (no empty space where it's hidden).
- Verify the cell's reuse identifier in Storyboard matches exactly what you're using in
dequeueReusableCell(withReuseIdentifier:).
Start with the hardcoded colored squares first—get those showing up, then gradually reintroduce your Firebase data logic. That way you can isolate whether the issue is with the collection view itself or the data flow.
内容的提问来源于stack exchange,提问作者Cody Berry

