NewRollButton.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // NewRollButton.m
  3. // MingMen
  4. //
  5. // Created by 罗云飞 on 2017/5/13.
  6. // Copyright © 2017年 罗云飞. All rights reserved.
  7. //
  8. #import "NewRollButton.h"
  9. #import "NewRollButtonCell.h"
  10. @interface NewRollButton ()<UICollectionViewDataSource,UICollectionViewDelegate>{
  11. NSMutableArray *dataArray;
  12. }
  13. @end
  14. @implementation NewRollButton
  15. - (id)initWithFrame:(CGRect)frame
  16. {
  17. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  18. flowLayout.minimumInteritemSpacing = 0; //列间距
  19. flowLayout.minimumLineSpacing = 0; //行间距
  20. [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
  21. self = [super initWithFrame:frame collectionViewLayout:flowLayout];
  22. if (self) {
  23. dataArray = [NSMutableArray array];
  24. //隐藏滑块
  25. self.showsHorizontalScrollIndicator = NO;
  26. self.showsVerticalScrollIndicator = NO;
  27. //设置代理
  28. self.delegate = self;
  29. self.dataSource = self;
  30. //设置背景颜色(默认黑色)
  31. self.backgroundColor = [UIColor clearColor];
  32. //注册单元格
  33. [self registerClass:[NewRollButtonCell class] forCellWithReuseIdentifier:@"Cell"];
  34. }
  35. return self;
  36. }
  37. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  38. {
  39. return 1;
  40. }
  41. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  42. {
  43. return dataArray.count;
  44. }
  45. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  46. {
  47. CGSize itemSize = CGSizeMake(0.0, 0.0);
  48. if (self.buttonType == rollButtonAndTitle) {
  49. itemSize.width = SCREEN_WIDTH/6;
  50. itemSize.height = self.frame.size.height;
  51. }
  52. else if (self.buttonType == rollButtonAndTitle) {
  53. itemSize.width = SCREEN_WIDTH/6;
  54. itemSize.height = self.frame.size.height;
  55. }
  56. return itemSize;
  57. }
  58. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  59. {
  60. NewRollButtonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
  61. if (dataArray.count>0) {
  62. [cell assignment:dataArray[indexPath.row] type:self.buttonType];
  63. }
  64. return cell;
  65. }
  66. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  67. {
  68. self.NewRollButtonCallback?self.NewRollButtonCallback(dataArray[indexPath.row]):nil;
  69. }
  70. - (void)reloadDatas:(NSMutableArray *)array
  71. {
  72. [dataArray removeAllObjects];
  73. [dataArray addObjectsFromArray:array];
  74. [self reloadData];
  75. }
  76. /*
  77. // Only override drawRect: if you perform custom drawing.
  78. // An empty implementation adversely affects performance during animation.
  79. - (void)drawRect:(CGRect)rect {
  80. // Drawing code
  81. }
  82. */
  83. @end