MJCollectionViewController.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // 代码地址: https://github.com/CoderMJLee/MJRefresh
  2. // MJCollectionViewController.m
  3. // MJRefreshExample
  4. //
  5. // Created by MJ Lee on 15/3/6.
  6. // Copyright (c) 2015年 小码哥. All rights reserved.
  7. //
  8. #import "MJCollectionViewController.h"
  9. #import "MJTestViewController.h"
  10. #import "UIViewController+Example.h"
  11. #import "MJRefresh.h"
  12. static const CGFloat MJDuration = 2.0;
  13. /**
  14. * 随机色
  15. */
  16. #define MJRandomColor [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1]
  17. @interface MJCollectionViewController()
  18. /** 存放假数据 */
  19. @property (strong, nonatomic) NSMutableArray *colors;
  20. @end
  21. @implementation MJCollectionViewController
  22. #pragma mark - 示例
  23. #pragma mark UICollectionView 上下拉刷新
  24. - (void)example21
  25. {
  26. __weak __typeof(self) weakSelf = self;
  27. // 下拉刷新
  28. [[MJRefreshNormalHeader headerWithRefreshingBlock:^{
  29. // 增加5条假数据
  30. for (int i = 0; i<10; i++) {
  31. [weakSelf.colors insertObject:MJRandomColor atIndex:0];
  32. }
  33. // 模拟延迟加载数据,因此2秒后才调用(真实开发中,可以移除这段gcd代码)
  34. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  35. [weakSelf.collectionView reloadData];
  36. // 结束刷新
  37. [weakSelf.collectionView.mj_header endRefreshing];
  38. });
  39. }] linkTo:self.collectionView];
  40. self.collectionView.mj_header.isCollectionViewAnimationBug = YES;
  41. // 简单粗暴版本
  42. // [self.collectionView.mj_header setAnimationDisabled];
  43. // 上拉刷新
  44. [[[[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  45. // 增加5条假数据
  46. for (int i = 0; i<5; i++) {
  47. [weakSelf.colors addObject:MJRandomColor];
  48. }
  49. // 模拟延迟加载数据,因此2秒后才调用(真实开发中,可以移除这段gcd代码)
  50. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  51. // [weakSelf.collectionView reloadData];
  52. // [weakSelf.collectionView.mj_footer endRefreshing];
  53. [weakSelf.collectionView performBatchUpdates:^{
  54. [weakSelf.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
  55. } completion:^(BOOL finished) {
  56. // 结束刷新
  57. [weakSelf.collectionView.mj_footer endRefreshing];
  58. }];
  59. });
  60. }] setAnimationDisabled]
  61. autoChangeTransparency:YES]
  62. linkTo:self.collectionView];
  63. [self.collectionView.mj_header beginRefreshing];
  64. }
  65. #pragma mark - 数据相关
  66. - (NSMutableArray *)colors
  67. {
  68. if (!_colors) {
  69. self.colors = [NSMutableArray array];
  70. }
  71. return _colors;
  72. }
  73. #pragma mark - 其他
  74. /**
  75. * 初始化
  76. */
  77. - (id)init
  78. {
  79. // UICollectionViewFlowLayout的初始化(与刷新控件无关)
  80. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  81. layout.itemSize = CGSizeMake(80, 80);
  82. layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
  83. layout.minimumInteritemSpacing = 20;
  84. layout.minimumLineSpacing = 20;
  85. return [self initWithCollectionViewLayout:layout];
  86. }
  87. static NSString *const MJCollectionViewCellIdentifier = @"color";
  88. - (void)viewDidLoad
  89. {
  90. [super viewDidLoad];
  91. MJPerformSelectorLeakWarning(
  92. [self performSelector:NSSelectorFromString(self.method) withObject:nil];
  93. );
  94. [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:MJCollectionViewCellIdentifier];
  95. }
  96. #pragma mark - collection数据源代理
  97. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  98. {
  99. // 设置尾部控件的显示和隐藏
  100. self.collectionView.mj_footer.hidden = self.colors.count == 0;
  101. return self.colors.count;
  102. }
  103. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  104. {
  105. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MJCollectionViewCellIdentifier forIndexPath:indexPath];
  106. cell.backgroundColor = self.colors[indexPath.row];
  107. return cell;
  108. }
  109. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  110. {
  111. MJTestViewController *test = [[MJTestViewController alloc] init];
  112. if (indexPath.row % 2) {
  113. [self.navigationController pushViewController:test animated:YES];
  114. } else {
  115. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:test];
  116. [self presentViewController:nav animated:YES completion:nil];
  117. }
  118. }
  119. @end