0
有人可以告訴我如何使用CoreData添加包含日期的部分?在Swift CoreData中添加用日期分隔的部分到UITableView
當我們寫入一些數據時,我有從CoreDate和AddTableViewController存儲所有數據的detailViewController。
詳細視圖控制器
import UIKit
import CoreData
class DetailViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate, AddTableViewControllerDelegate {
@IBOutlet weak var tableView: UITableView!
var detailTaskModel: Items!
var costsValues:Int!
@IBOutlet weak var sumLabel: UILabel!
// CoreData
let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext
var fetchedResultsController: NSFetchedResultsController = NSFetchedResultsController()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
fetchedResultsController = getFetchedResultsController()
fetchedResultsController.delegate = self
fetchedResultsController.performFetch(nil)
costsValues = getAccountCountSum()
self.tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// UITableViewDataSource
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return fetchedResultsController.sections![section].numberOfObjects
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: DetailTableViewCell = tableView.dequeueReusableCellWithIdentifier("detailCell") as DetailTableViewCell
let thisCost = fetchedResultsController.objectAtIndexPath(indexPath) as Costs
cell.nazwaWydatkuLabel.text = thisCost.costsName
cell.wartośćLabel.text = "\(thisCost.costsValue) zł"
return cell
}
@IBAction func addCostButtonPressed(sender: UIBarButtonItem) {
self.performSegueWithIdentifier("addCostVC", sender: self)
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return fetchedResultsController.sections!.count
}
override func viewDidAppear(animated: Bool) {
costsValues = getAccountCountSum()
refreshTable()
self.tableView.reloadData()
}
// CoreData Functions
func taskFetchRequest() -> NSFetchRequest {
let fetchRequest = NSFetchRequest(entityName: "Costs")
let sortDescriptor = NSSortDescriptor(key: "costsDate", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
return fetchRequest
}
func getFetchedResultsController() -> NSFetchedResultsController {
fetchedResultsController = NSFetchedResultsController(fetchRequest: taskFetchRequest(), managedObjectContext: managedObjectContext!, sectionNameKeyPath: nil, cacheName: nil)
return fetchedResultsController
}
// NSFetchedResultsControllerDelegate
func controllerDidChangeContent(controller: NSFetchedResultsController) {
tableView.reloadData()
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let thisCost = fetchedResultsController.objectAtIndexPath(indexPath) as Costs
managedObjectContext?.deleteObject(thisCost)
costsValues = getAccountCountSum()
refreshTable()
self.tableView.reloadData()
(UIApplication.sharedApplication().delegate as AppDelegate).saveContext()
}
// Sum count in all accouts
func getAccountCountSum() -> Int {
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let managedContext : NSManagedObjectContext = appDelegate.managedObjectContext!
var fetchRequest = NSFetchRequest(entityName: "Costs")
fetchRequest.returnsObjectsAsFaults = false
var results: NSArray = managedContext.executeFetchRequest(fetchRequest, error: nil)!
var accountsSum: Int = 0
for res in results {
var accountCount = res.valueForKey("costsValue") as Int
accountsSum += accountCount
}
self.sumLabel.text = String("\(costsValues) zł")
return accountsSum
}
func refreshTable() {
getAccountCountSum()
self.tableView.reloadData()
}
}
AddTableViewController
@IBAction func saveButtonTapped(sender: UIBarButtonItem) {
// CoreData Access
let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
var managedObjectContext = appDelegate.managedObjectContext
let entityDescription = NSEntityDescription.entityForName("Costs", inManagedObjectContext: managedObjectContext!)
let cost = Costs(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContext!)
cost.costsName = cellThreeNoteTextField.text
cost.costsValue = (cellOnePriceTextField.text).toInt()!
cost.costsDate = datePicker.date
// Saving data
appDelegate.saveContext()
var request = NSFetchRequest(entityName: "Costs")
var error:NSError? = nil
var results:NSArray = managedObjectContext!.executeFetchRequest(request, error: &error)!
for res in results {
println(res)
}
delegate?.refreshTable()
self.navigationController?.popViewControllerAnimated(true)
}