iOS--开发技巧iOS进阶指南iOS程序猿

Instagram 开源IGListKit学习

2017-08-03  本文已影响866人  Bool吖

一、IGListKit简介

IGListKit.png

IGListKit是Instagram推出的新的UICollectionView框架,使用数据驱动,旨在创造一个更快更灵活的列表控件。这个框架设计的非常好,完美符合高内聚、低耦合。
github地址:https://github.com/Instagram/IGListKit

二、IGListKit Feature

三、IGListKit应用

我们在使用IGListKit过程中,会发现不用在ViewController中实现UICollectionDataSource和UICollectionViewDelegate协议,取而代之的是SectionController来实现对应的方法:

- (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 个参数:

image.png
adapter.collectionView = collectionView
adapter.dataSource = self

这会将 collectionView 和 adapter 联系在一起。还将 self 设置为 adapter 的数据源,即遵守 IGListAdapterDataSource 协议。
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联系在一起:

- (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前后数据变化增删改移关系的一个算法,算是IGListKit的特色特点之一 。

实现差异比较需要自定义model遵守IGListDiffable协议并实现
- (nonnull id<NSObject>)diffIdentifier- (BOOL)isEqualToDiffableObject:(nullable id<IGListDiffable>)object两个方法。

四、Working Range

Working Range是我们可以指定左右的Working区间,干一些准备工作,比如下载图片。

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
上一篇下一篇

猜你喜欢

热点阅读