Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Cocoa勉強会関西#53
Search
cockscomb
December 14, 2013
Programming
3
570
Cocoa勉強会関西#53
Key Value Observationを利用するUITableViewDataSourceについて話しました。雑談コーナーもあります。
cockscomb
December 14, 2013
Tweet
Share
More Decks by cockscomb
See All by cockscomb
jq at the Shortcuts
cockscomb
1
1.8k
GraphQL放談
cockscomb
4
1.9k
GraphQL Highway
cockscomb
28
8.3k
吉田を支える技術
cockscomb
0
2.2k
コーポレートサイトを静的化してAmplify Consoleにデプロイする
cockscomb
0
3.4k
ユーザインターフェイスと非同期処理
cockscomb
5
1.8k
GUIアプリケーションの構造と設計
cockscomb
10
10k
イカリング2におけるシングルページアプリケーション
cockscomb
2
7.4k
あなたの知らない UIKit の世界 — UITableView に UITextView を置きたい
cockscomb
1
7.4k
Other Decks in Programming
See All in Programming
推しメソッドsource_locationのしくみを探る - はじめてRubyのコードを読んでみた
nobu09
2
350
Drawing Heighway’s Dragon- Recursive Function Rewrite- From Imperative Style in Pascal 64 To Functional Style in Scala 3
philipschwarz
PRO
0
140
ML.NETで始める機械学習
ymd65536
0
240
「個人開発マネタイズ大全」が教えてくれたこと
bani24884
1
280
Boos Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
540
PEPCは何を変えようとしていたのか
ken7253
3
300
DevNexus - Create AI Infused Java Apps with LangChain4j
kdubois
0
130
CDKを使ったPagerDuty連携インフラのテンプレート化
shibuya_shogo
0
120
Jakarta EE meets AI
ivargrimstad
0
570
楽しく向き合う例外対応
okutsu
0
720
Serverless Rust: Your Low-Risk Entry Point to Rust in Production (and the benefits are huge)
lmammino
1
160
もう少しテストを書きたいんじゃ〜 #phpstudy
o0h
PRO
19
4.2k
Featured
See All Featured
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
29
1.1k
How STYLIGHT went responsive
nonsquared
99
5.4k
The Power of CSS Pseudo Elements
geoffreycrofte
75
5.5k
Product Roadmaps are Hard
iamctodd
PRO
51
11k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.2k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
160
15k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
7
650
The Cost Of JavaScript in 2023
addyosmani
47
7.4k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3k
Optimizing for Happiness
mojombo
377
70k
Automating Front-end Workflow
addyosmani
1369
200k
Transcript
cockscomb
גࣜձࣾͯͳ Application Engineer Hatena Blog, Hatena Space cockscomb
Hatena Engineer Seminar #2 ͯͳʹ͓͚ΔϞμϯiOSΞϓϦ։ൃೖ
– @uzulla lυϥοάΞϯυυϩοϓࣦഊ͍ͬͯ͢͠%JT Γͭͭ$-*ͷDPDPB1PEΛࢍඒͨ͠ޙʹɺʮϚ εͰ৭ʑͰ͖Δ4#ศརɺ͋ͱ͜͜Μͱ͜Ͱ %O%͢Δͱόʔ͕ͷͼͨΓஔͰ͖ͯศརʂʯͬ ͯݴ͏ͷ͍͢͝IBUFOBUFDIz
– @kazuph lϞμϯʹॻ͔ͳ͔ͬͨ߹ͱॻ͍ͨ߹Λίʔ υͰൺֱͨͭ͠ͷͰઆ໌Λͯ͘͠ΕͯΔͷͰ ͍͢͝ḿΔIBUFOBUFDIz
͜Ε͕MVC™Ͱ͢
UITableViewDataSource
Bookmarks View BookmarkManager - (NSArray *)bookmarks TableViewController Refer Update
in TableViewController [[IBKMBookmarkManager sharedManager] reloadBookmarksWithBlock:^(NSError *error) { if (error) {
NSLog(@"Error: %@", error); } [self.tableView reloadData]; }];
Better Way [[IBKMBookmarkManager sharedManager] reloadBookmarksWithBlock:^(NSError *error) { if (error) {
NSLog(@"Error: %@", error); } }];
Key Value Observation
Observe [[IBKMBookmarkManager sharedManager] addObserver:self forKeyPath:@"bookmarks" options:NSKeyValueObservingOptionNew context:nil];
- (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]; } }
in Bookmarks Manager [[self mutableArrayValueForKey:@"bookmarks"] insertObjects:newBookmarks atIndexes:[NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, newBookmarks.count)]];
Mutable array proxy
Bookmarks View BookmarkManager - (NSArray)bookmarks TableViewController Observe Notify Update
Is it, eh?
Problems • Lightweight view controller • Copy and paste? •
Testability
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;
Observer Data Source • Useful for many occasions • DRY
• Testable • Applicable to UICollectionView
This is the MVC™
References Lighter View Controllers — objc.io Key-Value Coding and Observing
— objc.io Key-Value Observing — NSHipster
ࡶஊίʔφʔ
ʰiOS Core Data పఈೖʱ • ࠷ॳͷ͘Β͍ಡ·ͳ͍͍ͯ͘ • Core Dataʹ͍͓͓ͭͯΑͦཏత •
ຊޠͷใ͋·Γͳ͍ͷͰՁ͕ߴ͍ • ϚϧνεϨουͷ͜ͱॻ͍ͯͳ͍ • ύϑΥʔϚϯεͷ͜ͱॻ͍ͯͳ͍
iOS 7 ྑ͗͢Δ • CFAutorelease() • NSArray -firstObject • TextKit
• JavaScriptCore • Others
WebView JavaScript Bridging • - webView: shouldStartLoadWithRequest: navigationType: • Cordova
• WebViewJavascriptBridge • JSContext JSContext *ctx = [self.webView valueForKeyPath: @"documentView.webView.mainFrame.javaScriptContext"]; JSValue *three = [ctx evaluateScript:@“1+2"];
ੵۃ࠾༻த http://www.hatena.ne.jp/company/staff