简体   繁体   English

如何在集合视图单元格上显示coreData(swift4)

[英]how to display coreData on a collection view cell (swift4)

My code below would work if it was displayed on a label. 如果我的以下代码显示在标签上,则可以使用。 But in this function I am getting the error message Missing return in a function expected to return 'UICollectionViewCell'. 但是在此函数中,我收到错误消息:缺少预期返回“ UICollectionViewCell”的函数中的返回。 Putting the return cell in the for loop does not work. 将返回单元格放入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()
        }



    }




}

在此处输入图片说明

cellForItemAt method expects to return UICollectionViewCell in all cases. cellForItemAt方法期望在所有情况下都返回UICollectionViewCell。 In the else condition, return a default cell as below:- 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()
}

Note:-Register collection view with identifier defaultCcell 注意:-使用标识符defaultCcell注册收集视图

func getDefaultCell() -> UICollectionViewCell{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "defaultCcell", for: indexPath) as! UICollectionViewCell
return cell 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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