ios - CoreData,在表格视图中显示保存的旅程
问题描述
我正在尝试在表格视图中显示健身风格应用程序中记录的所有旅程的列表,以显示每次旅程的距离(布尔值)和日期(时间戳)。
目前我刚刚创建了一个变量来包含核心数据文件中的旅程。当我打印出旅程数组时,即使有一些记录的旅程,它也会在控制台中显示 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
子类,你应该使用 aNSFetchedResultsController
来获取持久化的对象。
您SavedJourneysViewController
必须具有对NSManagedObjectContext
用于获取Journey
对象的实例的引用。让我们假设您的viewContext
type 属性是从外部设置的,无论您在何处初始化.NSManagedObjectContext
SavedJourneysViewController
SavedJourneysViewController
您需要声明一个fetchedResultsController
in 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
}()
然后viewDidLoad
通过调用(例如)执行获取try? 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
}
更多关于NSFetchedResultsController
使用UITableViewController
-
推荐阅读
- firebase - iOS中Firebase动态链接的可靠性?
- javascript - javascript类构造函数初始化数组
- python - 在 Flask 应用程序中的视图 (html) 上显示 pyfolio 输出
- python - 如何去除图像中的显示器闪烁噪声?
- swift - Swift Combine - 数组上的前缀发布者
- json - 如何为jq中的每个内部数组元素打印对象元素
- python - 更改熊猫列中的值:SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame error
- here-api - 提高 HERE.com 地理编码器的精度
- reactjs - React、DotNet 核心本地化
- c# - Tcp xml 通信 C#