ios - CoreData,在表格 View 中显示保存的旅程

标签 ios swift core-data

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

目前,我刚刚创建了一个变量来包含核心数据文件中的旅程。当我打印出旅程数组时,即使有一些记录的旅程,它在控制台中显示 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 必须引用您将用于获取 Journey 对象的 NSManagedObjectContext 实例。假设您的 SavedJourneysViewController 中有一个 NSManagedObjectContext 类型的 viewContext 属性,该属性是从外部设置的,无论您在何处初始化 SavedJourneysViewController .

您需要在 SavedJourneysViewController 中声明一个 fetchedResultsController

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

然后通过调用 try? 在 viewDidLoad 中执行获取(例如) 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 结合使用的更多信息 -

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

相关文章:

ios - 更新 CoreData 中的项目不会刷新 tableView

ios - 将屏幕截图创建的 gif 文件保存到相机胶卷

ios - UITesting、XCTest 当前 ViewController 类

ios - swift 3 (SpriteKit) : Reseting GameScene doesn't deallocate

swift - Date.addingTimeInterval(_ :) and Date. advanced(by :)?)有什么区别

xcode - 核心数据迁移 : Attribute Mapping Value Expression

ios - 按下时 UIButton 不突出显示

ios - UIRefreshControl 在 Xamarin iOS 上崩溃

iOS 13 和 UINavigationBarDelegate::shouldPop()

objective-c - 使用 Restkit 手动添加 ManagedObject