繁体   English   中英

非空函数应该返回一个值

[英]Non-void function should return a value

我目前正在尝试制作日历,但此错误不断弹出。

我试过返回 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 }

非空函数应该返回一个值

如果你想返回 0 和 UICollectionViewCell,你应该返回它们

像这样

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}

首先,如果您使用的是UICollectionView ,则可能需要在其中显示至少 1 个UICollectionViewCell

为此,有UICollectionViewDataSource方法。

collectionView(_: numberOfItemsInSection:)返回0不会做任何有用的事情。 它就像我有一个collectionView并且不想在其中显示任何内容(直到并且除非有特定条件返回 0)。

因此,该方法必须返回要在collectionView显示的cells数。

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 1
}

现在出现了您面临的错误: Non-void function should return a value

错误是因为另一个UICollectionViewDataSource's方法,即collectionView(_: cellForItemAt:) 此方法期望您return将在collectionViewCell中可见的实际collectionView

在您添加的代码中,您只调用return 相反,您必须像这样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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM