MJCollectionViewController.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // 代码地址: https://github.com/CoderMJLee/MJRefresh
  2. // 代码地址: http://code4app.com/ios/%E5%BF%AB%E9%80%9F%E9%9B%86%E6%88%90%E4%B8%8B%E6%8B%89%E4%B8%8A%E6%8B%89%E5%88%B7%E6%96%B0/52326ce26803fabc46000000
  3. // MJCollectionViewController.m
  4. // MJRefreshExample
  5. //
  6. // Created by MJ Lee on 15/3/6.
  7. // Copyright (c) 2015年 小码哥. All rights reserved.
  8. //
  9. #import "MJCollectionViewController.h"
  10. #import "MJTestViewController.h"
  11. #import "UIViewController+Example.h"
  12. #import "MJRefresh.h"
  13. static const CGFloat MJDuration = 2.0;
  14. /**
  15. * 随机色
  16. */
  17. #define MJRandomColor [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1]
  18. @interface MJCollectionViewController()
  19. /** 存放假数据 */
  20. @property (strong, nonatomic) NSMutableArray *colors;
  21. @end
  22. @implementation MJCollectionViewController
  23. #pragma mark - 示例
  24. #pragma mark UICollectionView 上下拉刷新
  25. - (void)example21
  26. {
  27. __weak __typeof(self) weakSelf = self;
  28. // 下拉刷新
  29. self.collectionView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  30. // 增加5条假数据
  31. for (int i = 0; i<10; i++) {
  32. [weakSelf.colors insertObject:MJRandomColor atIndex:0];
  33. }
  34. // 模拟延迟加载数据,因此2秒后才调用(真实开发中,可以移除这段gcd代码)
  35. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  36. [weakSelf.collectionView reloadData];
  37. // 结束刷新
  38. [weakSelf.collectionView.mj_header endRefreshing];
  39. });
  40. }];
  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.backgroundColor = [UIColor whiteColor];
  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