NSFetchedResultsController 快速部分
Posted
技术标签:
【中文标题】NSFetchedResultsController 快速部分【英文标题】:NSFetchedResultsController swift sections 【发布时间】:2015-06-19 20:44:18 【问题描述】:我有一个表格视图,它从具有 3 个字段的 CoreData 实体中获取数据。 firstName: String, lastName: String 和 done:NSNumber(这是一个可以打开或关闭的 UISwitch)。
我想用名字和姓氏填充表格,但第一部分的名字是 Switch ON,第二部分的名字是 Switch OFF。
这是保存数据的代码:
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: - 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
我只是不知道如何根据“完成”属性将数据分成两个部分。
【问题讨论】:
【参考方案1】:首先,您需要按done
属性对List
对象进行排序,然后是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
为每个部分设置适当的标题。
【讨论】:
以上是关于NSFetchedResultsController 快速部分的主要内容,如果未能解决你的问题,请参考以下文章