简体   繁体   中英

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:

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

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

Note: Please conform to the naming convention that class names start with a capital letter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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