Why won't my Collection View Cells display in the iPhone Simulator?
In your viewDidLoad() Try:
viewDidLoad() {
ibCollectionView.dataSource = self
ibCollectionView.delegate = self
}
Add this to the top of your class:
class FluidIntakeMainMenuViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
You must use these functions in your ViewController class:
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {}
Collection View Cells not appearing
Did you set the CollectionViewController
to the storyboard identity inspector? :)
And I would try to call the reloadData()
after you change the data in the viewDidLoad
method.
Hope that helps
Collection View Cells not appearing in Collection View?
Just configure the collectionView
properly see below code and image:
Implement the delegate
methods of collectionView
:
class yourClassController: UICollectionViewDataSource, UICollectionViewDelegate {
override func numberOfSectionsInCollectionView(collectionView:
UICollectionView!) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView!,
numberOfItemsInSection section: Int) -> Int {
return yourArray.count
}
override func collectionView(collectionView: UICollectionView!,
cellForItemAtIndexPath indexPath: NSIndexPath!) ->
UICollectionViewCell! {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as CollectionViewCell
// Configure the cell
cell.backgroundColor = UIColor.blackColor()
cell.textLabel?.text = "\(indexPath.section):\(indexPath.row)"
cell.imageView?.image = UIImage(named: "circle")
return cell
}
Then from your storyboard
set the delegate
and datasource
by drag and drop see image:
Note: collectionView
appears when you do complete above formality with its relevant class.
UICollectionView is not displaying - bug?
For what you're trying to achieve using auto layout sounds like it would be your best option. You could alternatively set your UICollectionView
's frame to be in the bottom right corner like so:
collectionView!.frame = CGRect(x: self.view.frame.size.width - collectionView!.frame.size.width, y: self.view.frame.size.height - collectionView!.frame.size.height, width: 400, height: 400)
But this is more of a temporary fix and would have to be considered for each device's, and future device's, layout.
Collectionview not showing
Try this out.
In your cellForItemAtIndexPath
you must return both cells in order to show them both.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCellID", for: indexPath) as! MyButtonCell
let targetCell = targetCollection.dequeueReusableCell(withReuseIdentifier: "target", for: indexPath) as! MyButtonCell
if collectionView == self.collectionView {
// Setup here your cell
cell.callback = {
print("Button was tapped at \(indexPath)")
self.targetButtonTitles.append(self.buttonTitles[indexPath.item])
print(self.targetButtonTitles)
targetCell.targetButton.setTitle(self.targetButtonTitles[indexPath.item], for: [])
// do what you want when the button is tapped
}
cell.buttonOne.setTitle(buttonTitles[indexPath.item], for: [])
return cell
} else {
// Setup here your targetCell
return targetCell
}
Note that also if you have a different number of sections for each collectionView, you must return them independently.
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == self.collectionView {
return 1 // Number of sections of your collectionView
}
return 1 // Number of sections of your targetCollection
}
UICollectionView Not Showing Cells Completely. Last Cells Are Not Getting Shown Completely
Guys some time problem seems way bigger than they looks.
I figure it out and yes it solved mine.
I was searching the simple and short solution for this and I found same related question below - :
UICollectionView not able to scroll to see the entire last row
and the answer given by @Carpetfizz really worked for me.
I was using auto layout thats the reason it was solved ;
If you are not using auto layout it won't work for you.
How to I get three cells pr row in a UICollectionView. (Works on simulator, but not on phone)
To calculate the itemSize, you simply need to conform your ViewController
to UICollectionViewDelegateFlowLayout
protocol
and implement the below methods,
extension ViewController: UICollectionViewDelegateFlowLayout {
enum Constants {
static let interItemSpacing: CGFloat = 3.0
static let lineSpacing: CGFloat = 3.0
static let itemsPerRow = 3
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = (collectionView.bounds.width - (Constants.interItemSpacing * (CGFloat(Constants.itemsPerRow) - 1))) / CGFloat(Constants.itemsPerRow)
return CGSize(width: width, height: width)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return Constants.interItemSpacing
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return Constants.lineSpacing
}
}
In the above code, I've also created an enum Constants
to define the constant values required in the methods.
Also, specify the scrollDirection
in the storyboard
itself instead of doing it programatically.
UIcollectionview cell issue Swift
You need to extend your ViewController1
to conform to UICollectionViewDelegateFlowLayout
protocol. And implement methods to set size for each collection view cell like so:
class ViewController1: UICollectionViewController, UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView?.delegate = self
self.collectionView?.dataSource = self
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print(self.collectionView!.frame)
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "P", for: indexPath)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: self.collectionView!.frame.size.width, height: 120.0)
}
}
It has hard coded values, but you should get the idea. Collection View width should be the max size for each item, so they won't get outside the box.
Error message when setting UICollectionView data source and delegate with custom class
First and most important thing is to check the IBOutlet connection
that you made in your storyboard.
Try to remove the connection in storyboard and then reconnect it.
Secondly put a debugger on to check if it's still nil.
ibCollectionView.dataSource = self
XCode UICollectionViewCell appearing much smaller on simulator than in storyboard
try placing your setupUI in
viewDidAppear
instead onviewDidLoad
and what type of iPhone you run and what type of iPhone when you use storyboard?
well, if you use iPhone 5s in your storyboard it'll sure look bigger than 10+ right?
because iphone 5 has smaller resolution than 10
- You need to post your code, because i can't really assume what is your issue
Related Topics
Xcode Gm: No Swift Language for Os X Command Line Tool Project
Stanford Calculator App Keeps Crashing
Why Does the Compiler Not See the Default Code in a Protocol
Get Element from Array of Dictionaries According to Key
How to Retrieve Audio File from Parse Swift
Store Data in Custom Class Array in Core Data
Cannot Stop Background Music from Within Game Scenes, Swift 3/Spritekit
Could Not Cast Value of Type Uiview to [Customview] Using Xib
Declaring Conformance to @Objc Protocol in Empty Extension Breaks with Exc_Bad_Instruction
Avaudioplayer.Play() Works But Avaudioplayernode.Play() Fails
Generic Class with Class-Bound Constraint Cannot Be Parametrized by Class-Bound Protocol
Moving an Object Across the Screen at a Certain Speed.(Sprite Kit)
Swift: Filter a Dictionary with Array as Value
Value of Type 'Authdataresult' Has No Member 'Uid'
Using @Viewbuilder to Create Views Which Support Multiple Children
Swift "Retry" Logic on Request
Swift: How to Handle Video and Photo Results from a PHPicker