NSFetchedResultsController swift部分 [英] NSFetchedResultsController swift sections

查看:117
本文介绍了NSFetchedResultsController swift部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我想使用第一个和



这里是保存数据的代码:

p>

  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)
}
}

,这里是填充表视图的代码:

  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: - 表视图数据源

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!列表

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

return cell
}

}

我不知道如何将数据分成

解决方案

首先,需要对 / code>: $
let doneSortDescriptor = NSSortDescriptor(key:done,ascending:true)
let sortDescriptor = NSSortDescriptor(key:firstName,ascending:true)
fetchRequest.sortDescriptors = [doneSortDescriptor,sortDescriptor]

然后修改您的Fetched结果控制器它使用 done 属性定义节:

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

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


I have a table view that takes data from a CoreData entity with 3 fields. firstName: String, lastName: String and done:NSNumber (which is a UISwitch that can be turned ON or OFF).

I want to populate the table with the first and last names but first section is with names having the Switch ON, and a second section with names having the Switch OFF.

here is the code that saves the data:

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)
}
}

and here is the code that populates the table view:

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
}

}

I just can't figure out how to separate the data into the two sections based on the 'done' attribute.

解决方案

First, you need to sort your List objects by the done attribute, then the firstName:

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

Then amend your Fetched Results Controller to get it to use the done attribute to define the sections:

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

Finally, implement tableView:titleForHeaderInSection to set an appropriate title for each section.

这篇关于NSFetchedResultsController swift部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆