// // MJCollectionViewController.m // BaseProject // // Created by WindShan on 2016/12/7. // Copyright © 2016年 WindShan. All rights reserved. // #import "MJCollectionViewController.h" #define MJPerformSelectorLeakWarning(Stuff) \ do { \ _Pragma("clang diagnostic push") \ _Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \ Stuff; \ _Pragma("clang diagnostic pop") \ } while (0) static const CGFloat MJDuration = 2.0; /** * 随机色 */ #define MJRandomColor [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1] @interface MJCollectionViewController () /** 存放假数据 */ @property (strong, nonatomic) NSMutableArray *colors; @property (nonnull,strong) UICollectionView *collectionView; @end @implementation MJCollectionViewController #pragma mark - 示例 #pragma mark UICollectionView 上下拉刷新 - (void)example21 { __weak __typeof(self) weakSelf = self; // 下拉刷新 self.collectionView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{ // 增加5条假数据 for (int i = 0; i<10; i++) { [weakSelf.colors insertObject:MJRandomColor atIndex:0]; } // 模拟延迟加载数据,因此2秒后才调用(真实开发中,可以移除这段gcd代码) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [weakSelf.collectionView reloadData]; // 结束刷新 [weakSelf.collectionView.mj_header endRefreshing]; }); }]; [self.collectionView.mj_header beginRefreshing]; // 上拉刷新 self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{ // 增加5条假数据 for (int i = 0; i<5; i++) { [weakSelf.colors addObject:MJRandomColor]; } // 模拟延迟加载数据,因此2秒后才调用(真实开发中,可以移除这段gcd代码) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [weakSelf.collectionView reloadData]; // 结束刷新 [weakSelf.collectionView.mj_footer endRefreshing]; }); }]; // 默认先隐藏footer self.collectionView.mj_footer.hidden = YES; } #pragma mark - 数据相关 - (NSMutableArray *)colors { if (!_colors) { self.colors = [NSMutableArray array]; } return _colors; } static NSString *const MJCollectionViewCellIdentifier = @"color"; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // UICollectionViewFlowLayout的初始化(与刷新控件无关) UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.itemSize = CGSizeMake(80, 80); layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20); layout.minimumInteritemSpacing = 20; layout.minimumLineSpacing = 20; self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) collectionViewLayout:layout]; [self.view addSubview:self.collectionView]; self.collectionView.backgroundColor = [UIColor whiteColor]; [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:MJCollectionViewCellIdentifier]; [self setMethod:self.title]; // 根据方法名字调用该方法 MJPerformSelectorLeakWarning( [self performSelector:NSSelectorFromString(self.method) withObject:nil]; ); } static char MethodKey; - (void)setMethod:(NSString *)method { objc_setAssociatedObject(self, &MethodKey, method, OBJC_ASSOCIATION_COPY_NONATOMIC); } - (NSString *)method { return objc_getAssociatedObject(self, &MethodKey); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - collection数据源代理 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { // 设置尾部控件的显示和隐藏 //self.collectionView.mj_footer.hidden = self.colors.count == 0; return self.colors.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MJCollectionViewCellIdentifier forIndexPath:indexPath]; cell.backgroundColor = self.colors[indexPath.row]; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { // MJTestViewController *test = [[MJTestViewController alloc] init]; // if (indexPath.row % 2) { // [self.navigationController pushViewController:test animated:YES]; // } else { // UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:test]; // [self presentViewController:nav animated:YES completion:nil]; // } } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end