
IGListKit的管理者--IGListAdapter
如上圖所示,該框架直接通過IGListAdapter對象去管理UICollectionView并封裝了UICollectionViewDelegate、UICollectionViewDataSource的相關(guān)實現(xiàn),同時Adapter和Controller互相持有,用戶只需要實現(xiàn)IGListAdapterDataSource就可以實現(xiàn)一個靈活的列表。
一.屬性
typedef void (^IGListUpdaterCompletion)(BOOL finished);
//Adapter和Controller互相持有的同時,也負(fù)責(zé)管理著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是一個實現(xiàn)了IGListUpdatingDelegate協(xié)議的對象,負(fù)責(zé)處理row和section的刷新
@property (nonatomic, strong, readonly) id <IGListUpdatingDelegate> updater;
@property (nonatomic, assign) IGListExperiment experiments;
二.初始化方法
//workingRangeSize:是在可見對象之外的對象數(shù),當(dāng)他們接近可見時通知他們。 例如,如果屏幕
//上有3個對象,workingRangeSize為2, 將通知之前和之后的2個對象它們是否在工作范圍內(nèi)。
//滾動列表時當(dāng)對象進(jìn)入和退出時,范圍會更新
- (instancetype)initWithUpdater:(id <IGListUpdatingDelegate>)updater
viewController:(nullable UIViewController *)viewController
workingRangeSize:(NSInteger)workingRangeSize NS_DESIGNATED_INITIALIZER;
//關(guān)于該方法實現(xiàn)中的NSHashTable和NSMapTable可以參考:
//http://m.itdecent.cn/p/de71385930ba
//該方法默認(rèn)workingRangeSize:0,調(diào)用上面的方法
- (instancetype)initWithUpdater:(id <IGListUpdatingDelegate>)updater
viewController:(nullable UIViewController *)viewController;
三.一些方法
//從數(shù)據(jù)源的先前狀態(tài)執(zhí)行更新
- (void)performUpdatesAnimated:(BOOL)animated completion:(nullable IGListUpdaterCompletion)completion;
//立即重新加載數(shù)據(jù)源中的數(shù)據(jù),丟棄舊對象。
//不要使用此方法在沒有animations的情況下進(jìn)行更新,因為拆除和重建所有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;//返回當(dāng)前驅(qū)動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中,實現(xiàn)了
<UICollectionViewDataSource>,
<UICollectionViewDelegate>,
<UICollectionViewDelegateFlowLayout>,
<IGListCollectionViewDelegateLayout>
IGListAdapter通過管理IGListSectionController去實現(xiàn)UICollectionViewDelegate和UICollectionViewDataSource的相關(guān)實現(xiàn)。
通過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