MJCollectionViewController.m 4.4 KB

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