2
0

MJHorizontalCollectionViewController.m 3.5 KB

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