MJCollectionViewController.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. self.collectionView.mj_header= [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. }];
  40. self.collectionView.mj_header.isCollectionViewAnimationBug = YES;
  41. [self.collectionView.mj_header beginRefreshing];
  42. // 上拉刷新
  43. self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  44. // 增加5条假数据
  45. for (int i = 0; i<5; i++) {
  46. [weakSelf.colors addObject:MJRandomColor];
  47. }
  48. // 模拟延迟加载数据,因此2秒后才调用(真实开发中,可以移除这段gcd代码)
  49. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  50. [weakSelf.collectionView reloadData];
  51. // 结束刷新
  52. [weakSelf.collectionView.mj_footer endRefreshing];
  53. });
  54. }];
  55. // 默认先隐藏footer
  56. self.collectionView.mj_footer.hidden = YES;
  57. }
  58. #pragma mark - 数据相关
  59. - (NSMutableArray *)colors
  60. {
  61. if (!_colors) {
  62. self.colors = [NSMutableArray array];
  63. }
  64. return _colors;
  65. }
  66. #pragma mark - 其他
  67. /**
  68. * 初始化
  69. */
  70. - (id)init
  71. {
  72. // UICollectionViewFlowLayout的初始化(与刷新控件无关)
  73. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  74. layout.itemSize = CGSizeMake(80, 80);
  75. layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
  76. layout.minimumInteritemSpacing = 20;
  77. layout.minimumLineSpacing = 20;
  78. return [self initWithCollectionViewLayout:layout];
  79. }
  80. static NSString *const MJCollectionViewCellIdentifier = @"color";
  81. - (void)viewDidLoad
  82. {
  83. [super viewDidLoad];
  84. MJPerformSelectorLeakWarning(
  85. [self performSelector:NSSelectorFromString(self.method) withObject:nil];
  86. );
  87. [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:MJCollectionViewCellIdentifier];
  88. }
  89. #pragma mark - collection数据源代理
  90. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  91. {
  92. // 设置尾部控件的显示和隐藏
  93. self.collectionView.mj_footer.hidden = self.colors.count == 0;
  94. return self.colors.count;
  95. }
  96. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  97. {
  98. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MJCollectionViewCellIdentifier forIndexPath:indexPath];
  99. cell.backgroundColor = self.colors[indexPath.row];
  100. return cell;
  101. }
  102. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  103. {
  104. MJTestViewController *test = [[MJTestViewController alloc] init];
  105. if (indexPath.row % 2) {
  106. [self.navigationController pushViewController:test animated:YES];
  107. } else {
  108. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:test];
  109. [self presentViewController:nav animated:YES completion:nil];
  110. }
  111. }
  112. @end