S S I N G C AT E G O R I E S New hotness: myImage.setImage(myURL, size: CGSize(20,20)) Old and busted: 1. Download image 2. Resize image 3. Cache on device 4. Call setImage
N S • WatchKit is stringly typed when instantiating controllers: • presentControllerWithName("Event", context: e) • setRowTypes(rowTypes: [“EventType”, “DateType”]) • WatchKit is weakly typed when passing contexts: • override func awakeWithContext(context: AnyObject?) • Watch UI elements are write only • No inherent delegate pattern
N S • Instead of delegates: func numberOfSections() -> Int func numberOfRowsInSection(section: Int) -> Int func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell • We get setters: func setRowTypes(rowTypes: [AnyObject]) func setNumberOfRows(numberOfRows: Int, withRowType rowType: String) • Oh and BTW: row controllers are just NSObjects
N S • We end up with code that looks like this: table.setNumberOfRows(10, withRowType: “EventRowType”) for i in 0..<10 { let data = rowData[i] let rowController = table.rowControllerAtIndex(i) as! EventController rowController.homeTeamLabel.setText(...) rowController.awayTeamLabel.setText(...) }
N S • But it quickly gets more complicated table.setRowTypes(“DateRow”, “EventRow”, “EventRow”) for i in 0..<10 { let data = rowData[i] switch data { case is Event: let rowController = table.rowControllerAtIndex(i) as! EventController rowController.populate(data) break case is Date: let rowController = table.rowControllerAtIndex(i) as! DateController rowController.populate(data) break default: break } }
• Now when you reload data you can diff your old view model against your new view model to limit updates protocol Updatable { typealias T func updateFromOldValue(oldValue : T?, toNewValue newValue : T?) } extension WKInterfaceLabel : Updatable { func updateFromOldValue(oldValue : String?, toNewValue newValue : String? { if(newValue != oldValue) { self.setText(newValue) } } }
N S • We can even use this for tables protocol TableViewModel { var rowTypes : [String] { get } func rowViewModelAtIndex(index: Int) -> Any? func table(table: WKInterfaceTable, updateFromOldRowViewModel oldRowViewModel:Any?, toNewRowViewModel newRowViewModel:Any?, atIndex index: Int) }