PPImageScrollingCellView.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // PPImageScrollingCellView.m
  3. // PPImageScrollingTableViewDemo
  4. //
  5. // Created by popochess on 13/8/9.
  6. // Copyright (c) 2013年 popochess. All rights reserved.
  7. //
  8. #import "PPImageScrollingCellView.h"
  9. #import "PPCollectionViewCell.h"
  10. @interface PPImageScrollingCellView () <UICollectionViewDataSource, UICollectionViewDelegate>
  11. @property (strong, nonatomic) PPCollectionViewCell *myCollectionViewCell;
  12. @property (strong, nonatomic) UICollectionView *myCollectionView;
  13. @property (strong, nonatomic) NSArray *collectionImageData;
  14. @property (nonatomic) CGFloat imagetitleWidth;
  15. @property (nonatomic) CGFloat imagetitleHeight;
  16. @property (strong, nonatomic) UIColor *imageTilteBackgroundColor;
  17. @property (strong, nonatomic) UIColor *imageTilteTextColor;
  18. @end
  19. @implementation PPImageScrollingCellView
  20. - (id)initWithFrame:(CGRect)frame
  21. {
  22. self = [super initWithFrame:frame];
  23. if (self) {
  24. // Initialization code
  25. /* Set flowLayout for CollectionView*/
  26. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  27. flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  28. flowLayout.itemSize = CGSizeMake(100.0, 110.0);
  29. flowLayout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
  30. flowLayout.minimumLineSpacing = 10;
  31. /* Init and Set CollectionView */
  32. self.myCollectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flowLayout];
  33. self.myCollectionView.delegate = self;
  34. self.myCollectionView.dataSource = self;
  35. self.myCollectionView.showsHorizontalScrollIndicator = NO;
  36. [self.myCollectionView registerClass:[PPCollectionViewCell class] forCellWithReuseIdentifier:@"PPCollectionCell"];
  37. [self addSubview:_myCollectionView];
  38. }
  39. return self;
  40. }
  41. - (void) setImageData:(NSArray*)collectionImageData{
  42. _collectionImageData = collectionImageData;
  43. [_myCollectionView reloadData];
  44. }
  45. - (void) setBackgroundColor:(UIColor*)color{
  46. self.myCollectionView.backgroundColor = color;
  47. [_myCollectionView reloadData];
  48. }
  49. - (void) setImageTitleLabelWitdh:(CGFloat)width withHeight:(CGFloat)height{
  50. _imagetitleWidth = width;
  51. _imagetitleHeight = height;
  52. }
  53. - (void) setImageTitleTextColor:(UIColor*)textColor withBackgroundColor:(UIColor*)bgColor{
  54. _imageTilteTextColor = textColor;
  55. _imageTilteBackgroundColor = bgColor;
  56. }
  57. #pragma mark - UICollectionViewDataSource methods
  58. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  59. return 1;
  60. }
  61. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  62. return [self.collectionImageData count];
  63. }
  64. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  65. {
  66. PPCollectionViewCell *cell = (PPCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"PPCollectionCell" forIndexPath:indexPath];
  67. NSDictionary *imageDic = [self.collectionImageData objectAtIndex:[indexPath row]];
  68. [cell setImage:[UIImage imageNamed:[imageDic objectForKey:@"name"]]];
  69. [cell setImageTitleLabelWitdh:_imagetitleWidth withHeight:_imagetitleHeight];
  70. [cell setImageTitleTextColor:_imageTilteTextColor withBackgroundColor:_imageTilteBackgroundColor];
  71. [cell setTitle:[imageDic objectForKey:@"title"]];
  72. return cell;
  73. }
  74. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  75. [self.delegate collectionView:self didSelectImageItemAtIndexPath:(NSIndexPath*)indexPath];
  76. }
  77. /*
  78. // Only override drawRect: if you perform custom drawing.
  79. // An empty implementation adversely affects performance during animation.
  80. - (void)drawRect:(CGRect)rect
  81. {
  82. // Drawing code
  83. }
  84. */
  85. @end