I am currently trying to make a calendar and this error keeps on popping up.
I have tried return 0, and return UICollectionViewCell
.
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {return 0}
private func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {return }
Non-void function should return a value
If you want to return 0 and UICollectionViewCell, you should return them
Like this
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "nameOfIdentifier", for: indexPath)
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {return 0}
First of all, if you're using a UICollectionView
, you might need to show atleast 1 UICollectionViewCell
in it.
And for that very purpose there is UICollectionViewDataSource
methods.
Returning 0
in collectionView(_: numberOfItemsInSection:)
won't do anything useful. Its just like I've a collectionView
and don't want to show anything in it (until and unless there are specific conditions to return 0).
So, the method must return the number of cells
you want to show in the collectionView
.
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
Now comes the error you're facing: Non-void function should return a value .
The error is because of another UICollectionViewDataSource's
methods, ie collectionView(_: cellForItemAt:)
. This method expects you to return
the actual collectionViewCell
that'll be visible in the collectionView
.
In the code you added, you're only calling return
. Instead you must return
an instance of UICollectionViewCell
like so,
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YOUR_CELL_IDENTIFIER", for: indexPath)
return cell
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.