MJCollectionViewController.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. // 结束刷新
  53. [weakSelf.collectionView.mj_footer endRefreshing];
  54. });
  55. }] setAnimationDisabled]
  56. autoChangeTransparency:YES]
  57. linkTo:self.collectionView];
  58. }
  59. #pragma mark - 数据相关
  60. - (NSMutableArray *)colors
  61. {
  62. if (!_colors) {
  63. self.colors = [NSMutableArray array];
  64. }
  65. return _colors;
  66. }
  67. #pragma mark - 其他
  68. /**
  69. * 初始化
  70. */
  71. - (id)init
  72. {
  73. // UICollectionViewFlowLayout的初始化(与刷新控件无关)
  74. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  75. layout.itemSize = CGSizeMake(80, 80);
  76. layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
  77. layout.minimumInteritemSpacing = 20;
  78. layout.minimumLineSpacing = 20;
  79. return [self initWithCollectionViewLayout:layout];
  80. }
  81. static NSString *const MJCollectionViewCellIdentifier = @"color";
  82. - (void)viewDidLoad
  83. {
  84. [super viewDidLoad];
  85. MJPerformSelectorLeakWarning(
  86. [self performSelector:NSSelectorFromString(self.method) withObject:nil];
  87. );
  88. [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:MJCollectionViewCellIdentifier];
  89. }
  90. #pragma mark - collection数据源代理
  91. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  92. {
  93. // 设置尾部控件的显示和隐藏
  94. self.collectionView.mj_footer.hidden = self.colors.count == 0;
  95. return self.colors.count;
  96. }
  97. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  98. {
  99. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MJCollectionViewCellIdentifier forIndexPath:indexPath];
  100. cell.backgroundColor = self.colors[indexPath.row];
  101. return cell;
  102. }
  103. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  104. {
  105. MJTestViewController *test = [[MJTestViewController alloc] init];
  106. if (indexPath.row % 2) {
  107. [self.navigationController pushViewController:test animated:YES];
  108. } else {
  109. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:test];
  110. [self presentViewController:nav animated:YES completion:nil];
  111. }
  112. }
  113. @end