I am trying to display a list of all journeys recorded in a fitness-style app in a table view to show the distance (boolean) and date (timestamp) of each journey.
At the moment I have just created a variable to contain the Journeys from the core data file. When I print out the journeysArray, it shows 0 in the console even though there are some recorded journeys.
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
}
If Journey
is your NSManagedObject
subclass, you should use a NSFetchedResultsController
to fetch the persisted objects.
Your SavedJourneysViewController
must have a reference to the NSManagedObjectContext
instance that you'll use to fetch your Journey
objects. Let's assume that you have a viewContext
property of type NSManagedObjectContext
in your SavedJourneysViewController
that's being set from the outside, wherever you initialize your SavedJourneysViewController
.
You'll want to declare a 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
}()
Then perform fetch in viewDidLoad
(for example) by calling try? fetchedResultsController.performFetch()
try? fetchedResultsController.performFetch()
:
Then in numberOfRowsInSection
return fetchedResultsController.sections?[section].objects?.count ?? 0
fetchedResultsController.sections?[section].objects?.count ?? 0
:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return fetchedResultsController.sections?[section].objects?.count ?? 0
}
Don't forget about implementing func numberOfSections(in tableView: UITableView) -> Int
and returning fetchedResultsController.sections?.count ?? 0
fetchedResultsController.sections?.count ?? 0
:
func numberOfSections(in tableView: UITableView) -> Int {
return fetchedResultsController.sections?.count ?? 0
}
In cellForRowAt
, configure your cell with a Journey
object:
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
}
More about using NSFetchedResultsController
with UITableViewController
-
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.