I have CoreData Model:
And I try use fetchResultsController to display sections and rows. Sections should be from Category entity and rows should be from Items entity. How I can achieve this?
I use code:
override func numberOfSections(in tableView: UITableView) -> Int {
return fetchResultsController?.sections?.count ?? 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let sectionInfo = fetchResultsController.sections![section]
return sectionInfo.numberOfObjects
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ItemsTableViewCell
if let person = fetchResultsController?.object(at: indexPath) {
cell.textLabel?.text = person.name
}
return cell
}
But with this code I get only Category names. But I want to show Items related to special category. What I should change in
if let person = fetchResultsController?.object(at: indexPath) {
cell.textLabel?.text = person.name
}
to display Items for special Category?
My code for configuring fetchResultsController:
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.beginUpdates()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .insert: guard let indexPath = newIndexPath else { break }
tableView.insertRows(at: [indexPath], with: .fade)
case .delete: guard let indexPath = indexPath else { break }
tableView.deleteRows(at: [indexPath], with: .fade)
case .update: guard let indexPath = indexPath else { break }
tableView.reloadRows(at: [indexPath], with: .fade)
default:
tableView.reloadData()
}
categories = controller.fetchedObjects as! [Category]
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.endUpdates()
}
And:
var fetchResultsController: NSFetchedResultsController<Category>!
let request: NSFetchRequest<Category> = Category.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
fetchResultsController = NSFetchedResultsController<Category>(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
try? fetchResultsController?.performFetch()
fetchResultsController?.delegate = self
tableView.reloadData()
You need to base the frc on the Item
entity, and use the sectionNameKeyPath
to reference the Category
name:
var fetchResultsController: NSFetchedResultsController<Item>!
let request: NSFetchRequest<Item> = Item.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(key: "list.name", ascending: true)]
fetchResultsController = NSFetchedResultsController<Item>(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: “list.name”, cacheName: nil)
try? fetchResultsController?.performFetch()
fetchResultsController?.delegate = self
tableView.reloadData()
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.