NSFetchedResultsController swift 个部分

NSFetchedResultsController swift sections

我有一个 table 视图,它从具有 3 个字段的 CoreData 实体获取数据。 firstName: String, lastName: String 和 done:NSNumber(这是一个可以打开或关闭的 UISwitch)。

我想用名字和姓氏填充 table,但第一部分是开关打开的名字,第二部分是开关关闭的名字。

这里是保存数据的代码:

class ViewController: UIViewController {
var context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext!
var newItem: List? = nil

@IBOutlet weak var txtFirst: UITextField!
@IBOutlet weak var txtLast: UITextField!
@IBOutlet weak var switchDone: UISwitch!

override func viewDidLoad() {
    super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
@IBAction func cancelTapped(sender: AnyObject) {
    dismissVC()
}
@IBAction func saveTapped(sender: AnyObject) {
    let context = self.context
    let ent = NSEntityDescription.entityForName("List", inManagedObjectContext: context)

    let nItem = List(entity: ent!, insertIntoManagedObjectContext: context)
    nItem.firstName = txtFirst.text
    nItem.lastName = txtLast.text
    nItem.done = switchDone.on
    context.save(nil)        
    dismissVC()
}
func dismissVC() {
    navigationController?.popViewControllerAnimated(true)
}
}

这里是填充 table 视图的代码:

class TableViewController: UITableViewController, NSFetchedResultsControllerDelegate {

var context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext!

var frc: NSFetchedResultsController = NSFetchedResultsController()

func getFetchedResultsController() -> NSFetchedResultsController {
    frc = NSFetchedResultsController(fetchRequest: listFetchRequest(), managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
    return frc
}
func listFetchRequest() -> NSFetchRequest {
    let fetchRequest = NSFetchRequest(entityName: "List")
    let sortDescriptor = NSSortDescriptor(key: "firstName", ascending: true)
    fetchRequest.sortDescriptors = [sortDescriptor]
    return fetchRequest
}
override func viewDidLoad() {
    super.viewDidLoad()
    frc = getFetchedResultsController()
    frc.delegate = self
    frc.performFetch(nil)
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func controllerDidChangeContent(controller: NSFetchedResultsController) {
    tableView.reloadData()
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    let numberOfSections = frc.sections?.count
    return numberOfSections!
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let numberOfRowsInSection = frc.sections?[section].numberOfObjects
    return numberOfRowsInSection!
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
    let list = frc.objectAtIndexPath(indexPath) as! List

    cell.textLabel?.text = list.firstName
    cell.detailTextLabel?.text = list.lastName

    return cell
}

}

我只是想不出如何根据 'done' 属性将数据分成两部分。

首先,您需要按 done 属性对 List objects 进行排序,然后是 firstName:

let doneSortDescriptor = NSSortDescriptor(key: "done", ascending: true)
let sortDescriptor = NSSortDescriptor(key: "firstName", ascending: true)
fetchRequest.sortDescriptors = [doneSortDescriptor, sortDescriptor]

然后修改您的 Fetched Results Controller,使其使用 done 属性来定义部分:

frc = NSFetchedResultsController(fetchRequest: listFetchRequest(), managedObjectContext: context, sectionNameKeyPath: "done", cacheName: nil)

最后,实施 tableView:titleForHeaderInSection 为每个部分设置适当的标题。