MJHorizontalCollectionViewController.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. // MJHorizontalCollectionViewController.m
  4. // MJRefreshExample
  5. //
  6. // Created by MJ Lee on 15/3/6.
  7. // Copyright (c) 2015年 小码哥. All rights reserved.
  8. //
  9. #import "MJHorizontalCollectionViewController.h"
  10. #import "MJTestViewController.h"
  11. #import "UIViewController+Example.h"
  12. #import "MJRefresh.h"
  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 MJHorizontalCollectionViewController()
  18. /** 存放假数据 */
  19. @property (strong, nonatomic) NSMutableArray *colors;
  20. @end
  21. @implementation MJHorizontalCollectionViewController
  22. #pragma mark - 示例
  23. #pragma mark UICollectionView 左拉刷新
  24. - (void)example42 {
  25. __weak __typeof(self) weakSelf = self;
  26. // 左拉刷新
  27. self.collectionView.mj_trailer = [MJRefreshNormalTrailer trailerWithRefreshingBlock:^{
  28. MJTestViewController *test = [[MJTestViewController alloc] init];
  29. [weakSelf.navigationController pushViewController:test animated:YES];
  30. [weakSelf.collectionView.mj_trailer endRefreshing];
  31. }];
  32. }
  33. #pragma mark - 数据相关
  34. - (NSMutableArray *)colors {
  35. if (!_colors) {
  36. self.colors = [NSMutableArray array];
  37. for (int i = 0; i < 5; i++) {
  38. [self.colors addObject:MJRandomColor];
  39. }
  40. }
  41. return _colors;
  42. }
  43. #pragma mark - 其他
  44. /**
  45. * 初始化
  46. */
  47. - (instancetype)init {
  48. // UICollectionViewFlowLayout的初始化(与刷新控件无关)
  49. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  50. layout.itemSize = CGSizeMake(UIScreen.mainScreen.bounds.size.width, 200);
  51. layout.minimumInteritemSpacing = 0;
  52. layout.minimumLineSpacing = 0;
  53. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  54. return [self initWithCollectionViewLayout:layout];
  55. }
  56. static NSString *const MJCollectionViewCellIdentifier = @"color";
  57. - (void)viewDidLoad {
  58. [super viewDidLoad];
  59. self.view.backgroundColor = [UIColor whiteColor];
  60. self.collectionView.frame = CGRectMake(0, 150, UIScreen.mainScreen.bounds.size.width, 200);
  61. self.collectionView.pagingEnabled = YES;
  62. self.collectionView.backgroundColor = [UIColor whiteColor];
  63. MJPerformSelectorLeakWarning([self performSelector:NSSelectorFromString(self.method) withObject:nil];);
  64. [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:MJCollectionViewCellIdentifier];
  65. }
  66. #pragma mark - collection数据源代理
  67. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  68. return self.colors.count;
  69. }
  70. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  71. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MJCollectionViewCellIdentifier forIndexPath:indexPath];
  72. cell.backgroundColor = self.colors[indexPath.row];
  73. return cell;
  74. }
  75. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  76. MJTestViewController *test = [[MJTestViewController alloc] init];
  77. if (indexPath.row % 2) {
  78. [self.navigationController pushViewController:test animated:YES];
  79. } else {
  80. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:test];
  81. [self presentViewController:nav animated:YES completion:nil];
  82. }
  83. }
  84. @end