Instagram 開源IGListKit學習

一境输、IGListKit簡介

IGListKit.png

IGListKit是Instagram推出的新的UICollectionView框架,使用數(shù)據(jù)驅動颖系,旨在創(chuàng)造一個更快更靈活的列表控件嗅剖。這個框架設計的非常好,完美符合高內(nèi)聚嘁扼、低耦合窗悯。
github地址:https://github.com/Instagram/IGListKit

二、IGListKit Feature

  • 不要再調用 performBatchUpdates(_:, completion:) 或者 reloadData() 偷拔。
  • 具有可重用的單元和組件的更好的體系結構
  • 創(chuàng)建具有多個數(shù)據(jù)類型的集合
  • 解耦的差異算法
  • 充分的單元測試
  • 為你的模塊自定義你的差異行為
  • 可擴展的API
  • 使用Objective-C編寫,具有全Swift互操作支持

三亏钩、IGListKit應用

我們在使用IGListKit過程中莲绰,會發(fā)現(xiàn)不用在ViewController中實現(xiàn)UICollectionDataSource和UICollectionViewDelegate協(xié)議,取而代之的是SectionController來實現(xiàn)對應的方法:

- (NSInteger)numberOfItems {
    return 3;
}

- (CGSize)sizeForItemAtIndex:(NSInteger)index {
    return CGSizeMake(self.collectionContext.containerSize.width, 55);
}

- (UICollectionViewCell *)cellForItemAtIndex:(NSInteger)index {
    LabelCollectionViewCell *cell = [self.collectionContext dequeueReusableCellOfClass:NSClassFromString(@"LabelCollectionViewCell") forSectionController:self atIndex:index];
    cell.text = [NSString stringWithFormat:@"%@ section:%ld  cell:%ld", self.object.name, self.section, index];
    return cell;
}

IGListAdapter 初始化方法有 3 個參數(shù):

  • updater 是一個實現(xiàn)了 IGListUpdatingDelegate 協(xié)議的對象, 它負責處理 row 和 section 的刷新姑丑。IGListAdapterUpdater 有一個默認實現(xiàn)蛤签,剛好給我們用。
  • viewController 是一個 UIViewController 栅哀,它擁有這個 adapter震肮。 這個 view controller 后面會用于導航到別的 view controllers称龙。
  • workingRangeSize 是 warking range 的大小。允許你為那些不在可見范圍內(nèi)的 section 準備內(nèi)容戳晌。
    這是IGListKit結構:
image.png
adapter.collectionView = collectionView
adapter.dataSource = self

這會將 collectionView 和 adapter 聯(lián)系在一起鲫尊。還將 self 設置為 adapter 的數(shù)據(jù)源,即遵守 IGListAdapterDataSource 協(xié)議。
UICollectionDataSource和UICollectionViewDelegate在adapter中指定相應的代理:

- (void)setCollectionView:(UICollectionView *)collectionView {
    IGAssertMainThread();
    if (_collectionView != collectionView || _collectionView.dataSource != self) {
        static NSMapTable<UICollectionView *, IGListAdapter *> *globalCollectionViewAdapterMap = nil;
        if (globalCollectionViewAdapterMap == nil) {
            globalCollectionViewAdapterMap = [NSMapTable weakToWeakObjectsMapTable];
        }
        [globalCollectionViewAdapterMap removeObjectForKey:_collectionView];
        [[globalCollectionViewAdapterMap objectForKey:collectionView] setCollectionView:nil];
        [globalCollectionViewAdapterMap setObject:self forKey:collectionView];
        _registeredCellClasses = [NSMutableSet new];
        _registeredNibNames = [NSMutableSet new];
        _registeredSupplementaryViewIdentifiers = [NSMutableSet new];
        _registeredSupplementaryViewNibNames = [NSMutableSet new];
        _collectionView = collectionView;
        _collectionView.dataSource = self;
        [_collectionView.collectionViewLayout invalidateLayout];
        [self updateCollectionViewDelegate];
        [self updateAfterPublicSettingsChange];
    }
}

在IGListAdapter+UICollectionView中我們可以看到SectionController與UICollectionDataSource聯(lián)系在一起:

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.sectionMap.objects.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    IGListSectionController * sectionController = [self sectionControllerForSection:section];
    IGAssert(sectionController != nil, @"Nil section controller for section %zi for item %@. Check your -diffIdentifier and -isEqual: implementations.",
            section, [self.sectionMap objectForSection:section]);
    const NSInteger numberOfItems = [sectionController numberOfItems];
    IGAssert(numberOfItems >= 0, @"Cannot return negative number of items %zi for section controller %@.", numberOfItems, sectionController);
    return numberOfItems;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    IGListSectionController *sectionController = [self sectionControllerForSection:indexPath.section];
    _isDequeuingCell = YES;
    UICollectionViewCell *cell = [sectionController cellForItemAtIndex:indexPath.item];
    _isDequeuingCell = NO;
    IGAssert(cell != nil, @"Returned a nil cell at indexPath <%@> from section controller: <%@>", indexPath, sectionController);
    [self mapView:cell toSectionController:sectionController];
    return cell;
}

三沦偎、IGListDiffable and Equality

IGListDiffable使用的是Paul Heckel 的A technique for isolating differences between files 的算法疫向,簡單來說這個算法就是計算collectionView前后數(shù)據(jù)變化增刪改移關系的一個算法,算是IGListKit的特色特點之一 豪嚎。

實現(xiàn)差異比較需要自定義model遵守IGListDiffable協(xié)議并實現(xiàn)
- (nonnull id<NSObject>)diffIdentifier- (BOOL)isEqualToDiffableObject:(nullable id<IGListDiffable>)object兩個方法搔驼。

四、Working Range

Working Range是我們可以指定左右的Working區(qū)間侈询,干一些準備工作舌涨,比如下載圖片。

Working Range 方法:

/**
通知代理Section Controller將要進入Working Range扔字。
 */
- (void)listAdapter:(IGListAdapter *)listAdapter sectionControllerWillEnterWorkingRange:(IGListSectionController *)sectionController;

/**
通知代理Section Controller退出Working Range囊嘉。
 */
- (void)listAdapter:(IGListAdapter *)listAdapter sectionControllerDidExitWorkingRange:(IGListSectionController *)sectionController;


image.png
image.png
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市啦租,隨后出現(xiàn)的幾起案子哗伯,更是在濱河造成了極大的恐慌,老刑警劉巖篷角,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件焊刹,死亡現(xiàn)場離奇詭異,居然都是意外死亡恳蹲,警方通過查閱死者的電腦和手機虐块,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來嘉蕾,“玉大人贺奠,你說我怎么就攤上這事〈沓溃” “怎么了儡率?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長以清。 經(jīng)常有香客問我儿普,道長,這世上最難降的妖魔是什么掷倔? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任眉孩,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘浪汪。我一直安慰自己巴柿,他們只是感情好,可當我...
    茶點故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布死遭。 她就那樣靜靜地躺著广恢,像睡著了一般。 火紅的嫁衣襯著肌膚如雪殃姓。 梳的紋絲不亂的頭發(fā)上袁波,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天,我揣著相機與錄音蜗侈,去河邊找鬼篷牌。 笑死,一個胖子當著我的面吹牛踏幻,可吹牛的內(nèi)容都是我干的枷颊。 我是一名探鬼主播,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼该面,長吁一口氣:“原來是場噩夢啊……” “哼夭苗!你這毒婦竟也來了?” 一聲冷哼從身側響起隔缀,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤题造,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后猾瘸,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體界赔,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年牵触,在試婚紗的時候發(fā)現(xiàn)自己被綠了淮悼。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡揽思,死狀恐怖袜腥,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情钉汗,我是刑警寧澤羹令,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站损痰,受9級特大地震影響特恬,放射性物質發(fā)生泄漏。R本人自食惡果不足惜徐钠,卻給世界環(huán)境...
    茶點故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望役首。 院中可真熱鬧尝丐,春花似錦显拜、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至失息,卻和暖如春譬淳,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背盹兢。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工邻梆, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人绎秒。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓浦妄,卻偏偏與公主長得像,于是被迫代替她去往敵國和親见芹。 傳聞我的和親對象是個殘疾皇子剂娄,可洞房花燭夜當晚...
    茶點故事閱讀 45,675評論 2 359

推薦閱讀更多精彩內(nèi)容