一境输、IGListKit簡介
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結構:
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;