- 自动计算行高,缓存行高
- 支持设置cell的上下左右间隙
- 控制器减负最有效处理,根治MVC问题。不用写数据源方法,cell的点击事件逻辑分离到各cell中,逻辑清晰,代码不臃肿,代码复用耦合性少。
- 在cell中编写事件处理逻辑,可以直接获取CollectionView对象和ViewController对象,不用担心循环引用问题。消除一层层的代理传递,和通知传递数据
- 初始化:
MCollectionView *collectionView = [[MCollectionView alloc] initWithScrollDirection:UICollectionViewScrollDirectionVertical delegate:nil];
[self.view addSubview:collectionView];
-
cell中实现
- (void)setModel_mc:(NSObject *)model
方法设置cell数据 -
model中:实现
- (NSString *)reuseViewName_mc
方法,返回cell的类名 -
数据源:
MCollectionViewSectionModel *section1 = [MCollectionViewSectionModel new];
section1.itemModels = 第一组模型
section2.itemModels = 第二组模型
section3.itemModels = 第三组模型
NSMutableArray *sectionModels = [NSMutableArray arrayWithArray:@[section1, section2, section3]];
self.sectionModels = sectionModels;
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
注:- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
方法也不用写,点击Cell的处理逻辑就写在Cell中。此库会调用Cell的didSelectItem_mc
方法,并随后响应mDelegate
的didSelectItemAtIndexPath代理方法