Skip to content

解决MVC架构控制器臃肿的问题,模块化编程,高内聚低耦合

License

Notifications You must be signed in to change notification settings

Wejua/MCollectionView

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

MCollectionView

UICollectionView的封装:

  1. 自动计算行高,缓存行高
  2. 支持设置cell的上下左右间隙
  3. 控制器减负最有效处理,根治MVC问题。不用写数据源方法,cell的点击事件逻辑分离到各cell中,逻辑清晰,代码不臃肿,代码复用耦合性少。
  4. 在cell中编写事件处理逻辑,可以直接获取CollectionView对象和ViewController对象,不用担心循环引用问题。消除一层层的代理传递,和通知传递数据

使用方法:

  1. 初始化:
MCollectionView *collectionView = [[MCollectionView alloc] initWithScrollDirection:UICollectionViewScrollDirectionVertical delegate:nil];
[self.view addSubview:collectionView];
  1. cell中实现- (void)setModel_mc:(NSObject *)model方法设置cell数据

  2. model中:实现- (NSString *)reuseViewName_mc方法,返回cell的类名

  3. 数据源:

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代理方法

About

解决MVC架构控制器臃肿的问题,模块化编程,高内聚低耦合

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published