简体   繁体   English

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

[英]coreData not displaying on collection view cell (swift4)

This code right here display a array on a collection view. 此处的代码在集合视图上显示一个数组。 It works exactly how I want it to. 它完全按照我想要的方式工作。

   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
    }
}

在此处输入图片说明

In my 2nd code. 在我的第二代码中。 The code is slightly alerted to call core data and display on the cells. 稍微提醒代码以调用核心数据并在单元格上显示。 As you can see in the photo there are no cells visible. 如您在照片中看到的,没有可见的单元格。 All I want to do is have the core data be displayed exactly how I was able to do in the first code set. 我要做的就是将核心数据准确显示在第一个代码集中的操作方式。

       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
    }
}

在此处输入图片说明

CORE DATA 核心数据

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

        }
    }

}

First of all do not return an optional in fetchObject() , return an empty array on failure: 首先不要在fetchObject()中返回可选fetchObject() ,失败时返回一个空数组:

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

Second of all, to answer the question, you have to call fetchObject() in viewDidLoad to load the data 其次,要回答这个问题,您必须在viewDidLoad调用fetchObject()来加载数据

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

Note: Please conform to the naming convention that class names start with a capital letter. 注意:请遵守命名约定,即类名以大写字母开头。

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

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