我正在尝试在表格视图中显示健身风格的应用程序中记录的所有旅程的列表,以显示每个旅程的距离(布尔值)和日期(时间戳记)。

目前,我刚刚创建了一个变量,以包含来自核心数据文件的旅程。当我打印travelsArray时,即使有记录的旅程,它在控制台中仍显示为0。

import UIKit
import CoreData

class SavedJourneysViewController: UITableViewController {

    var journeyArray: [Journey] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print(journeyArray.count)
        return journeyArray.count

    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "JourneyItem", for: indexPath)
        return cell

    }

最佳答案

如果Journey是您的NSManagedObject子类,则应使用NSFetchedResultsController来获取持久对象。

SavedJourneysViewController必须具有对NSManagedObjectContext实例的引用,该实例将用于获取Journey对象。假设您在viewContext中有一个NSManagedObjectContext类型的SavedJourneysViewController属性,该属性是从外部设置的,而无论您初始化SavedJourneysViewController的位置如何。

您需要在fetchedResultsController中声明一个SavedJourneysViewController

private lazy var fetchedResultsController: NSFetchedResultsController<Journey> = {
    let fetchRequest: NSFetchRequest< Journey > = Journey.fetchRequest()
    let sortDescriptor = NSSortDescriptor(keyPath: \Journey.date, ascending: true)
    fetchRequest.sortDescriptors = [sortDescriptor]
    let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: viewContext, sectionNameKeyPath: nil, cacheName: nil)
    return fetchedResultsController
}()


然后通过调用viewDidLoadtry? fetchedResultsController.performFetch()中执行获取(例如):

然后在numberOfRowsInSection中返回fetchedResultsController.sections?[section].objects?.count ?? 0

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return fetchedResultsController.sections?[section].objects?.count ?? 0
}


不要忘记实现func numberOfSections(in tableView: UITableView) -> Int并返回fetchedResultsController.sections?.count ?? 0

func numberOfSections(in tableView: UITableView) -> Int {
    return fetchedResultsController.sections?.count ?? 0
}


cellForRowAt中,使用Journey对象配置单元格:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = let cell = tableView.dequeueReusableCell(withIdentifier: "JourneyItem", for: indexPath)
    guard let journey = fetchedResultsController.sections?[indexPath.section].objects?[indexPath.row] as? Journey else {
        return cell
    }
    // handle cell configuration
    cell.textLabel?.text = String(journey.distance)
    return cell
}


有关将NSFetchedResultsControllerUITableViewController结合使用的更多信息-


https://cocoacasts.com/populate-a-table-view-with-nsfetchedresultscontroller-and-swift-3
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreData/nsfetchedresultscontroller.html

关于ios - CoreData,在表格 View 中显示保存的旅程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55904749/

10-10 07:28