【发布时间】:2018-07-08 22:12:45
【问题描述】:
如果下面的代码显示在标签上,它会起作用。但是在这个函数中,我收到错误消息 Missing return in a function expected to return 'UICollectionViewCell'。将返回单元格放在 for 循环中不起作用。
import UIKit
class collectionVIEW: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var users = [User]()
@IBOutlet var theIssues: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
// users = cdHandler.fetchObject()!
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return users.count
}
func getDefaultCell() -> UICollectionViewCell{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! whyCollectionViewCell
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if cdHandler.fetchObject() != nil {
users = cdHandler.fetchObject()!
for c in users {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! whyCollectionViewCell
cell.general.text = "\((cell.general.text)!)+\((c.userName)!)"
return cell
}
} else {
return getDefaultCell()
}
}
}
【问题讨论】:
-
关于截图:请阅读错误信息并比较
dequeue...两行。提示:区分大小写很重要。cellForItem中的重复循环仍然没有意义。 -
@vadian 我尝试更改 dequeue 但没有效果我用我的所有代码更新了问题。
-
请学习了解表格视图的工作原理。
cellForItemAt被为每一行分别调用多次。users数组必须填充到cellForItemAt之外。提取和重复循环是无意义的。
标签: ios core-data uicollectionview swift4