如果标签显示在下面,我的代码可以正常工作。但是在这个函数中,我收到错误消息Missing return in a function is return 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()
}
}
}
答案 0 :(得分:0)
cellForItemAt
方法期望在所有情况下返回UICollectionViewCell。在else
条件下,返回默认单元格,如下所示: -
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()
}
注意:-Register集合视图,标识为defaultCcell
func getDefaultCell() -> UICollectionViewCell{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "defaultCcell", for: indexPath) as! UICollectionViewCell
return cell
}