【发布时间】:2015-11-11 14:50:31
【问题描述】:
我在使用 Swift 中的 Core Data 在表格视图中显示按部分分组的数据时遇到了一些麻烦。
我将用一个例子来说明这个问题。让我们假设三个类:订单、产品和服务。类 Order 具有属性“日期”以及与 Product 和 Service 的一对多关系。
对于每个订单,应创建一个部分以显示在特定日期订购的所有产品和服务。
使用 NSFetchedResultsController 根据属性日期获取所有订单。这提供了正确数量的部分。 但是,由于“numberOfRowsInSection”是基于订单数量而不是产品和服务数量,因此在每个部分的单独行中显示所有产品和服务时遇到问题。
下面是说明我的问题的代码。希望任何人都可以帮助我走向正确的方向。
提前致谢,
杰拉德
import UIKit
import CoreData
class ExampleViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate {
@IBOutlet weak var tableView: UITableView!
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
var fetchedResultController: NSFetchedResultsController = NSFetchedResultsController()
var orders = [Order]()
override func viewDidLoad() {
super.viewDidLoad()
fetchedResultController.delegate = self
tableView.dataSource = self
tableView.delegate = self
fetchData()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return fetchedResultController.sections?.count ?? 0
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let sectionInfo = fetchedResultController.sections![section] as NSFetchedResultsSectionInfo
return sectionInfo.numberOfObjects
// Unfortunatelly, this provides not the total number of products and services per section
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let textCellIdentifier = "ExampleTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! ExampleTableViewCell
let order = orders[indexPath.row] // Data fetched using NSFetchedResultsController
let products = order.products!.allObjects as! [Product] // Swift Array
let services = order.services!.allObjects as! [Service] // Swift Array
// Each product and each service should be displayed in separate rows.
// Instead, all products and services are displayed in a single row as below
var displaytext = ""
for product in products {
displaytext += product.name! + "\n"
}
for service in services {
displaytext += service.name! + "\n"
}
cell.orderLabel.text = displaytext // display text in ExampleTableViewCell
return cell
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if let sections = fetchedResultController.sections {
let currentSection = sections[section]
return currentSection.name
}
return nil
}
func orderFetchRequest() -> NSFetchRequest {
let fetchRequest = NSFetchRequest(entityName: "Order")
let sortDescriptor = NSSortDescriptor(key: "date", ascending: true)
let predicate = NSPredicate(format: "date >= %@ AND date <= %@", startDate, endDate) // startDate and endData are defined elsewhere
fetchRequest.sortDescriptors = [sortDescriptor]
fetchRequest.predicate = predicate
return fetchRequest
}
func fetchData() {
let fetchRequest = orderFetchRequest()
fetchedResultController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath: "date", cacheName: nil)
do {
orders = try managedObjectContext.executeFetchRequest(fetchRequest) as! [Order]
}
catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
do {
try fetchedResultController.performFetch()
}
catch _ {
}
}
}
【问题讨论】:
-
所以您希望每个订单有一个部分,并且在每个部分中为所有产品行,然后为所有服务行? FRC 不会轻易帮助您。它想要显示订单并深入了解产品和服务......
-
这正是我想要的。实际上,我希望可以获取父类 Order 的数据(使用 NSSortDescriptor 和 NSPredicate),并根据子类 Product 和 Service 的实例数提供 Table View 每个部分的行数。当 NSFetchedResultsController 无法(轻松)实现这一点时,是否有另一种方法来执行此操作?
标签: swift uitableview core-data nsfetchedresultscontroller