diff --git a/HardcoreDataDemo/HardcoreDataDemo/ObjectListObserverDemoViewController.swift b/HardcoreDataDemo/HardcoreDataDemo/ObjectListObserverDemoViewController.swift index 0e28857..74de276 100644 --- a/HardcoreDataDemo/HardcoreDataDemo/ObjectListObserverDemoViewController.swift +++ b/HardcoreDataDemo/HardcoreDataDemo/ObjectListObserverDemoViewController.swift @@ -74,8 +74,11 @@ class ObjectListObserverDemoViewController: UITableViewController, ManagedObject override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("PaletteTableViewCell") as! PaletteTableViewCell + let palette = paletteList[indexPath] - cell.setHue(palette.hue, saturation: palette.saturation, brightness: palette.brightness) + cell.colorView?.backgroundColor = palette.color + cell.label?.text = palette.colorText + return cell } @@ -132,11 +135,7 @@ class ObjectListObserverDemoViewController: UITableViewController, ManagedObject func managedObjectList(listController: ManagedObjectListController, didUpdateObject object: Palette, atIndexPath indexPath: NSIndexPath) { - if let cell = self.tableView.cellForRowAtIndexPath(indexPath) as? PaletteTableViewCell { - - let palette = paletteList[indexPath] - cell.setHue(palette.hue, saturation: palette.saturation, brightness: palette.brightness) - } + self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) } func managedObjectList(listController: ManagedObjectListController, didMoveObject object: Palette, fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) { @@ -176,10 +175,7 @@ class ObjectListObserverDemoViewController: UITableViewController, ManagedObject for _ in 0 ... 2 { let palette = transaction.create(Palette) - palette.hue = Int32(arc4random_uniform(360)) - palette.saturation = 1.0 - palette.brightness = 0.5 - palette.dateAdded = NSDate() + palette.setInitialValues() } transaction.commit { (result) -> Void in } diff --git a/HardcoreDataDemo/HardcoreDataDemo/ObjectObserverDemoViewController.swift b/HardcoreDataDemo/HardcoreDataDemo/ObjectObserverDemoViewController.swift index d33f8da..02db67f 100644 --- a/HardcoreDataDemo/HardcoreDataDemo/ObjectObserverDemoViewController.swift +++ b/HardcoreDataDemo/HardcoreDataDemo/ObjectObserverDemoViewController.swift @@ -35,10 +35,7 @@ class ObjectObserverDemoViewController: UIViewController, ManagedObjectObserver HardcoreData.beginSynchronous { (transaction) -> Void in let palette = transaction.create(Palette) - palette.hue = Int32(arc4random_uniform(360)) - palette.saturation = 1.0 - palette.brightness = 0.5 - palette.dateAdded = NSDate() + palette.setInitialValues() transaction.commitAndWait() } @@ -156,15 +153,12 @@ class ObjectObserverDemoViewController: UIViewController, ManagedObjectObserver func reloadPaletteInfo(palette: Palette) { - let color = UIColor( - hue: CGFloat(palette.hue) / 360.0, - saturation: CGFloat(palette.saturation), - brightness: CGFloat(palette.brightness), - alpha: 1.0 - ) - self.colorNameLabel?.textColor = color self.colorNameLabel?.text = palette.colorName + + let color = palette.color + self.colorNameLabel?.textColor = color self.colorView?.backgroundColor = color + self.hsbLabel?.text = "H: \(palette.hue)˚, S: \(round(palette.saturation * 100.0))%, B: \(round(palette.brightness * 100.0))%" let dateString = NSDateFormatter.localizedStringFromDate( diff --git a/HardcoreDataDemo/HardcoreDataDemo/Palette.swift b/HardcoreDataDemo/HardcoreDataDemo/Palette.swift index 5ba43e6..443a2a4 100644 --- a/HardcoreDataDemo/HardcoreDataDemo/Palette.swift +++ b/HardcoreDataDemo/HardcoreDataDemo/Palette.swift @@ -7,6 +7,7 @@ // import Foundation +import UIKit import CoreData class Palette: NSManagedObject { @@ -55,4 +56,27 @@ class Palette: NSManagedObject { self.didChangeValueForKey(key) } } + + var color: UIColor { + + return UIColor( + hue: CGFloat(self.hue) / 360.0, + saturation: CGFloat(self.saturation), + brightness: CGFloat(self.brightness), + alpha: 1.0 + ) + } + + var colorText: String { + + return "H: \(self.hue)˚, S: \(round(self.saturation * 100.0))%, B: \(round(self.brightness * 100.0))%" + } + + func setInitialValues() { + + self.hue = Int32(arc4random_uniform(360)) + self.saturation = 1.0 + self.brightness = 0.5 + self.dateAdded = NSDate() + } } diff --git a/HardcoreDataDemo/HardcoreDataDemo/PaletteTableViewCell.swift b/HardcoreDataDemo/HardcoreDataDemo/PaletteTableViewCell.swift index a616293..3ed057c 100644 --- a/HardcoreDataDemo/HardcoreDataDemo/PaletteTableViewCell.swift +++ b/HardcoreDataDemo/HardcoreDataDemo/PaletteTableViewCell.swift @@ -12,15 +12,4 @@ class PaletteTableViewCell: UITableViewCell { @IBOutlet weak var colorView: UIView? @IBOutlet weak var label: UILabel? - - func setHue(hue: Int32, saturation: Float, brightness: Float) { - - let color = UIColor( - hue: CGFloat(hue) / 360.0, - saturation: CGFloat(saturation), - brightness: CGFloat(brightness), - alpha: 1.0) - self.colorView?.backgroundColor = color - self.label?.text = "H: \(hue)˚, S: \(round(saturation * 100.0))%, B: \(round(brightness * 100.0))%" - } }