Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cocoa勉強会関西#53

cockscomb
December 14, 2013

 Cocoa勉強会関西#53

Key Value Observationを利用するUITableViewDataSourceについて話しました。雑談コーナーもあります。

cockscomb

December 14, 2013
Tweet

More Decks by cockscomb

Other Decks in Programming

Transcript

  1. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if

    (object == [IBKMBookmarkManager sharedManager] && [keyPath isEqualToString:@"bookmarks"]) { NSIndexSet *indexSet = change[NSKeyValueChangeIndexesKey]; NSKeyValueChange changeKind = (NSKeyValueChange)[change[NSKeyValueChangeKindKey] integerValue]; ! NSMutableArray *indexPaths = [NSMutableArray array]; [indexSet enumerateIndexesUsingBlock:^(NSUInteger index, BOOL *stop) { [indexPaths addObject:[NSIndexPath indexPathForRow:index inSection:0]]; }]; ! [self.tableView beginUpdates]; if (changeKind == NSKeyValueChangeInsertion) { [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic]; } else if (changeKind == NSKeyValueChangeRemoval) { [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic]; } else if (changeKind == NSKeyValueChangeReplacement) { [self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic]; } [self.tableView endUpdates]; } }
  2. The Best Way self.observerDataSource = [[ObserverTableViewDataSource alloc] initWithObserved:[IBKMBookmarkManager sharedManager] keyPath:NSStringFromSelector(@selector(bookmarks))

    tableView:self.tableView superDataSource:self]; ! self.observerDataSource.configureCell = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath, id object) { static NSString *const CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; ! ... ! return cell; }; ! self.tableView.dataSource = self.observerDataSource;
  3. Observer Data Source • Useful for many occasions • DRY

    • Testable • Applicable to UICollectionView
  4. ʰiOS Core Data పఈೖ໳ʱ • ࠷ॳͷ൒෼͘Β͍͸ಡ·ͳͯ͘΋͍͍ • Core Dataʹ͍͓͓ͭͯΑͦ໢ཏత •

    ೔ຊޠͷ৘ใ͋·Γͳ͍ͷͰՁ஋͕ߴ͍ • ϚϧνεϨουͷ͜ͱ͸ॻ͍ͯͳ͍ • ύϑΥʔϚϯεͷ͜ͱ΋ॻ͍ͯͳ͍
  5. WebView JavaScript Bridging • - webView:
 shouldStartLoadWithRequest:
 navigationType: • Cordova

    • WebViewJavascriptBridge • JSContext JSContext *ctx = [self.webView valueForKeyPath: @"documentView.webView.mainFrame.javaScriptContext"]; JSValue *three = [ctx evaluateScript:@“1+2"];