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
网友评论