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
560
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.6k
GraphQL放談
cockscomb
4
1.8k
GraphQL Highway
cockscomb
27
8k
吉田を支える技術
cockscomb
0
2k
コーポレートサイトを静的化してAmplify Consoleにデプロイする
cockscomb
0
3.3k
ユーザインターフェイスと非同期処理
cockscomb
5
1.7k
GUIアプリケーションの構造と設計
cockscomb
10
9.9k
イカリング2におけるシングルページアプリケーション
cockscomb
2
7.2k
あなたの知らない UIKit の世界 — UITableView に UITextView を置きたい
cockscomb
1
7.2k
Other Decks in Programming
See All in Programming
今こそ始める、CDKコンストラクトライブラリ開発 ― 入門から実践まで
tmokmss
0
370
データのマスタが変わっても継続的に分析したい!
tjmtmmnk
1
110
データサイエンスやデータエンジニアリングの求人サイトの個人開発
billyio
0
700
Kotlin Collection関数をマスター
masayukisuda
0
3.8k
キッティングツールによる自動化でセットアップ時間を1時間から3分に短縮
mot_techtalk
2
180
リプレイスを安心安全に 〜段階的リプレイスと等価比較〜/Safe and Secure Replacement ~ Phased Replacement and Equivalent Comparison ~
shun0624
1
310
どうせキレイに書けない処理は逆にAIに書いてもらうほうが良い説 / #kyotojs 22
potato4d
3
200
Xcode 16のPreviewModifierと@Previewableを活用した効率的なプレビュー方法の考察
ojun9
1
120
Mastering Developer Experience: A Roadmap for Success 【開発生産性Conference 2024】
findyinc
0
240
Get started with Compose Multiplatform!
ogi2ogi
0
1.3k
Tap it! Shake it! Fling it! Sheep it! The Gesture Animations Dance!
kwakeuijin
0
150
【Go言語】golangci-lintの使い方
tomo1227
0
220
Featured
See All Featured
Atom: Resistance is Futile
akmur
260
25k
Designing on Purpose - Digital PM Summit 2013
jponch
113
6.6k
KATA
mclloyd
18
12k
The Straight Up "How To Draw Better" Workshop
denniskardys
229
130k
What's in a price? How to price your products and services
michaelherold
238
11k
Visualization
eitanlees
137
14k
Large-scale JavaScript Application Architecture
addyosmani
506
110k
Music & Morning Musume
bryan
43
5.8k
Building an army of robots
kneath
300
42k
Infographics Made Easy
chrislema
238
18k
Documentation Writing (for coders)
carmenintech
62
4.1k
10 Git Anti Patterns You Should be Aware of
lemiorhan
651
58k
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