簡體   English   中英

coreData未顯示在集合視圖單元格上(swift4)

[英]coreData not displaying on collection view cell (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
    }
}

在此處輸入圖片說明

在我的第二代碼中。 稍微提醒代碼以調用核心數據並在單元格上顯示。 如您在照片中看到的,沒有可見的單元格。 我要做的就是將核心數據准確顯示在第一個代碼集中的操作方式。

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

在此處輸入圖片說明

核心數據

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

        }
    }

}

首先不要在fetchObject()中返回可選fetchObject() ,失敗時返回一個空數組:

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

其次,要回答這個問題,您必須在viewDidLoad調用fetchObject()來加載數據

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

注意:請遵守命名約定,即類名以大寫字母開頭。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM