[英]Non-void function should return a value
I am currently trying to make a calendar and this error keeps on popping up.我目前正在尝试制作日历,但此错误不断弹出。
I have tried return 0, and return UICollectionViewCell
.我试过返回 0,并返回
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如果你想返回 0 和 UICollectionViewCell,你应该返回它们
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.首先,如果您使用的是
UICollectionView
,则可能需要在其中显示至少 1 个UICollectionViewCell
。
And for that very purpose there is UICollectionViewDataSource
methods.为此,有
UICollectionViewDataSource
方法。
Returning 0
in collectionView(_: numberOfItemsInSection:)
won't do anything useful.在
collectionView(_: numberOfItemsInSection:)
返回0
不会做任何有用的事情。 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).它就像我有一个
collectionView
并且不想在其中显示任何内容(直到并且除非有特定条件返回 0)。
So, the method must return the number of cells
you want to show in the collectionView
.因此,该方法必须返回要在
collectionView
显示的cells
数。
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
Now comes the error you're facing: Non-void function should return a value .现在出现了您面临的错误: Non-void function should return a value 。
The error is because of another UICollectionViewDataSource's
methods, ie collectionView(_: cellForItemAt:)
.错误是因为另一个
UICollectionViewDataSource's
方法,即collectionView(_: cellForItemAt:)
。 This method expects you to return
the actual collectionViewCell
that'll be visible in the collectionView
.此方法期望您
return
将在collectionViewCell
中可见的实际collectionView
。
In the code you added, you're only calling return
.在您添加的代码中,您只调用
return
。 Instead you must return
an instance of UICollectionViewCell
like so,相反,您必须像这样
return
UICollectionViewCell
的实例,
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YOUR_CELL_IDENTIFIER", for: indexPath)
return cell
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.