coreData不显示在集合视图单元格(swift4)

时间:2018-02-04 02:14:42

标签: ios core-data uicollectionview uicollectionviewcell swift4

此代码在此处显示集合视图上的数组。它完全符合我的要求。

   import UIKit
import CoreData

class collectionVIEW: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{
    var sam = ["3","j"]
        var users = [User]()
    @IBOutlet var theIssues: UICollectionView!

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

         let cell =  collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! whyCollectionViewCell
    //            cell.general.text = users[indexPath.row].userName
          cell.general.text = sam[indexPath.row]
        return cell
    }
}

enter image description here

在我的第二段代码中。稍微提醒代码调用核心数据并在单元格上显示。正如您在照片中看到的那样,没有可见的细胞。我想要做的就是让核心数据准确显示我在第一个代码集中的表现。

       import UIKit
import CoreData

class collectionVIEW: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{
    var sam = ["3","j"]
        var users = [User]()
    @IBOutlet var theIssues: UICollectionView!

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! whyCollectionViewCell
 cell.general.text = users[indexPath.row].userName
        //           cell.general.text = sam[indexPath.row]
        return cell
    }
}

enter image description here

核心数据

import UIKit

import CoreData

class cdHandler: NSObject {

    private class func getContext() -> NSManagedObjectContext {
        let appdeleagetzz = UIApplication.shared.delegate as! AppDelegate
        return appdeleagetzz.persistentContainer.viewContext

    }

    class func saveObject(userName: String) -> Bool {
        let context = getContext()
        let entity = NSEntityDescription.entity(forEntityName: "User", in: context)

        let managedObject = NSManagedObject(entity: entity!, insertInto: context)

        managedObject.setValue(userName, forKey: "userName")


        do {
            try context.save()
            return true

        } catch {
            return false

        }
    }


    class func fetchObject() -> [User]? {
        let context = getContext()
        var users: [User]? = nil

        do {
            users = try context.fetch(User.fetchRequest())



            return users


        } catch {
            return users

        }
    }

}

1 个答案:

答案 0 :(得分:0)

首先不要在fetchObject()中返回一个可选项,在失败时返回一个空数组:

class func fetchObject() -> [User] {
     do {
        let context = getContext()
        return try context.fetch(User.fetchRequest())
     } catch {
         return [User]()
     }
}

其次,要回答这个问题,您必须致电fetchObject()中的viewDidLoad来加载数据

func viewDidLoad() {
    super.viewDidLoad()
    users = cdHandler.fetchObject()
    theIssues.reloadData()
}

注意:请遵守类名以大写字母开头的命名约定。