IGList之IGListAdapter

作者: 皮蛋豆腐酱油 | 来源:发表于2019-04-15 18:40 被阅读0次

IGListKit的管理者--IGListAdapter

如上图所示,该框架直接通过IGListAdapter对象去管理UICollectionView并封装了UICollectionViewDelegate、UICollectionViewDataSource的相关实现,同时Adapter和Controller互相持有,用户只需要实现IGListAdapterDataSource就可以实现一个灵活的列表。

一.属性

typedef void (^IGListUpdaterCompletion)(BOOL finished);
//Adapter和Controller互相持有的同时,也负责管理着SectionController。
@property (nonatomic, nullable, weak) UIViewController *viewController;
@property (nonatomic, nullable, weak) UICollectionView *collectionView;
@property (nonatomic, nullable, weak) id <IGListAdapterDataSource> dataSource;
@property (nonatomic, nullable, weak) id <IGListAdapterDelegate> delegate;
@property (nonatomic, nullable, weak) id <UICollectionViewDelegate> collectionViewDelegate;
@property (nonatomic, nullable, weak) id <UIScrollViewDelegate> scrollViewDelegate;
@property (nonatomic, nullable, weak) id <IGListAdapterMoveDelegate> moveDelegate NS_AVAILABLE_IOS(9_0);
//updater是一个实现了IGListUpdatingDelegate协议的对象,负责处理row和section的刷新
@property (nonatomic, strong, readonly) id <IGListUpdatingDelegate> updater;
@property (nonatomic, assign) IGListExperiment experiments;

二.初始化方法

//workingRangeSize:是在可见对象之外的对象数,当他们接近可见时通知他们。 例如,如果屏幕
//上有3个对象,workingRangeSize为2, 将通知之前和之后的2个对象它们是否在工作范围内。 
//滚动列表时当对象进入和退出时,范围会更新
- (instancetype)initWithUpdater:(id <IGListUpdatingDelegate>)updater
                 viewController:(nullable UIViewController *)viewController
               workingRangeSize:(NSInteger)workingRangeSize NS_DESIGNATED_INITIALIZER;
//关于该方法实现中的NSHashTable和NSMapTable可以参考:
//https://www.jianshu.com/p/de71385930ba
//该方法默认workingRangeSize:0,调用上面的方法
- (instancetype)initWithUpdater:(id <IGListUpdatingDelegate>)updater
                 viewController:(nullable UIViewController *)viewController;

三.一些方法

//从数据源的先前状态执行更新
- (void)performUpdatesAnimated:(BOOL)animated completion:(nullable IGListUpdaterCompletion)completion;
//立即重新加载数据源中的数据,丢弃旧对象。
//不要使用此方法在没有animations的情况下进行更新,因为拆除和重建所有section controllers可能非常昂贵
//使用` -  [IGListAdapter performUpdatesAnimated:completion]`代替
- (void)reloadDataWithCompletion:(nullable IGListUpdaterCompletion)completion;
- (void)reloadObjects:(NSArray *)objects;
- (nullable IGListSectionController *)sectionControllerForSection:(NSInteger)section;
- (NSInteger)sectionForSectionController:(IGListSectionController *)sectionController;
- (__kindof IGListSectionController * _Nullable)sectionControllerForObject:(id)object;
- (nullable id)objectForSectionController:(IGListSectionController *)sectionController;
- (nullable id)objectAtSection:(NSInteger)section;
- (NSInteger)sectionForObject:(id)object;
- (NSArray *)objects;//返回当前驱动adapter的所有对象的一个copy
- (NSArray<IGListSectionController *> *)visibleSectionControllers;
- (NSArray *)visibleObjects;
- (NSArray<UICollectionViewCell *> *)visibleCellsForObject:(id)object;
- (void)scrollToObject:(id)object
    supplementaryKinds:(nullable NSArray<NSString *> *)supplementaryKinds
       scrollDirection:(UICollectionViewScrollDirection)scrollDirection
        scrollPosition:(UICollectionViewScrollPosition)scrollPosition
              animated:(BOOL)animated;
//滚动到list adapter中的指定对象。
- (CGSize)sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
- (CGSize)sizeForSupplementaryViewOfKind:(NSString *)elementKind
                             atIndexPath:(NSIndexPath *)indexPath;
- (void)addUpdateListener:(id<IGListAdapterUpdateListener>)updateListener;
- (void)removeUpdateListener:(id<IGListAdapterUpdateListener>)updateListener;

四.IGListAdapter对UICollectionView的管理

在类IGListAdapter+UICollectionView中,实现了
<UICollectionViewDataSource>,
<UICollectionViewDelegate>,
<UICollectionViewDelegateFlowLayout>,
<IGListCollectionViewDelegateLayout>
IGListAdapter通过管理IGListSectionController去实现UICollectionViewDelegate和UICollectionViewDataSource的相关实现。
通过IGListSectionController也通过IGListBindingSectionControllerDataSource去管理着UICollectionViewCell、ViewModel以及Cell的展示。

这些方法里很多都用到了sectionMap,说一下这个属性:

@interface IGListSectionMap ()

//这两个maps 都允许快速查找objects, list objects, and indexes
//object : sectionController
@property (nonatomic, strong, readonly, nonnull) NSMapTable<id, IGListSectionController *> *objectToSectionControllerMap;
//sectionController : index
@property (nonatomic, strong, readonly, nonnull) NSMapTable<IGListSectionController *, NSNumber *> *sectionControllerToSectionMap;

@property (nonatomic, strong, nonnull) NSMutableArray *mObjects;

@end

参考:https://www.jianshu.com/p/3517619085f7

相关文章

  • IGList之IGListAdapter

    IGListKit的管理者--IGListAdapter 如上图所示,该框架直接通过IGListAdapter对象...

  • 2017优秀框架

    http://www.cocoachina.com/ios/20170208/18647.html1.IGList...

  • 好用的开发库

    IGListKit 作者是 Instagram EngineeringInstagram 程序员做的,IGList...

  • 开源库阅读笔记(未整理)

    IGListAdapter 抽象UIConllectionView 为 一个个sections 对象 即 IGLi...

  • 开源框架ALTableKit

    IGList用了一段时间了确实比较好用,但是发现把tableview 全部替换成collectionview成本很...

  • IGList实现支付宝首页

    IGListKit实现支付宝首页简述 前言 IGListKit是GitHub上Instagram开源的UIColl...

  • 数据源Diff算法分析

    在IGList中有一个非常神奇的功能,就是可以根据数据源直接算出列表变化,采用update的方式更新列表,不需要每...

  • 十之

    博学之,审问之,慎思之,明辨之,笃行之。 励志之,健身之,涅槃之,弘毅之,自强之!

  • 读记|唐诗人:诗心煎红尘(二)

    愈之挫之 险之退之 借之济之 忠之犯之 勇之夺之 衰之立之 坚之韧之 载之言之 一代宗师 成之传之 字曰子厚 道解...

  • 《寄君归》

    思之念之 见之不忘 吾亦求之 求之不得 吾亦念之 兮之盼之 来之归之 欲予离之 得之兮之 心思念之 盼来归之 归之...

网友评论

    本文标题:IGList之IGListAdapter

    本文链接:https://www.haomeiwen.com/subject/bdmuwqtx.html