猎豆优选

LDMorePicCollectionCell.m 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // LDMorePicCollectionCell.m
  3. // YouHuiProject
  4. //
  5. // Created by jcymac on 2018/4/24.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "LDMorePicCollectionCell.h"
  9. #import "LDMorePicCollectionViewCell.h"
  10. #import "LDMorePicCollectionModel.h"
  11. @interface LDMorePicCollectionCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  12. @property(nonatomic,strong)UICollectionView *collectionView;
  13. @property (nonatomic, strong) NSMutableArray *dataArr;
  14. @end
  15. @implementation LDMorePicCollectionCell
  16. static NSInteger sumCell=0;
  17. - (instancetype)initWithFrame:(CGRect)frame {
  18. self = [super initWithFrame:frame];
  19. if (self) {
  20. self.backgroundColor = [UIColor whiteColor];
  21. self.clipsToBounds = YES;
  22. }
  23. return self;
  24. }
  25. - (void)awakeFromNib {
  26. [super awakeFromNib];
  27. // Initialization code
  28. }
  29. - (void)setModelDatas:(NSArray *)dataArr {
  30. self.dataArr = [NSMutableArray arrayWithArray:dataArr];
  31. sumCell=self.dataArr.count;
  32. _collectionView=nil;
  33. for (UIView *view in self.subviews) {
  34. if ([view isKindOfClass:[UICollectionView class]]) {
  35. [view removeFromSuperview];
  36. }
  37. }
  38. [self addSubview:self.collectionView];
  39. // [self.collectionView reloadData];
  40. }
  41. #pragma mark -UICollectionViewDataSource
  42. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  43. if (self.dataArr==nil||self.dataArr.count==0) {
  44. return 0;
  45. }
  46. return self.dataArr.count;
  47. }
  48. - ( UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  49. LDMorePicCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"picCollectionViewCell" forIndexPath:indexPath];
  50. if (!cell) {
  51. }
  52. LDMorePicCollectionModel *model=self.dataArr[indexPath.row];
  53. cell.titleLabel.text=model.name;
  54. // [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@""]];
  55. [cell.picImageView sd_setFadeImageWithURL:[NSURL URLWithString:model.img] placeholderImage:nil options:0 progress:nil completed:nil];
  56. return cell;
  57. }
  58. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
  59. return 0;
  60. }
  61. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
  62. return 0;
  63. }
  64. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  65. if (self.delegate && [self.delegate respondsToSelector:@selector(YHMorePicCollectionCellDidSelectedItem:)]) {
  66. [self.delegate YHMorePicCollectionCellDidSelectedItem:indexPath.row];
  67. }
  68. }
  69. - (UICollectionView *)collectionView {
  70. if (!_collectionView) {
  71. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  72. if (sumCell==0) {
  73. sumCell=4;
  74. }
  75. NSInteger offHeight2=0;
  76. if (iPhone5) {
  77. offHeight2=10;
  78. }
  79. NSInteger Crow=1;
  80. NSInteger Coff=-1;
  81. NSInteger CrowItemSum=5;
  82. Coff=sumCell%CrowItemSum;
  83. if (Coff!=0) {
  84. CrowItemSum=4;
  85. Coff=sumCell%CrowItemSum;
  86. if (Coff!=0) {
  87. Coff=sumCell%3;
  88. CrowItemSum=3;
  89. if (Coff!=0) {
  90. CrowItemSum=5;
  91. }
  92. }
  93. }
  94. Crow=sumCell/CrowItemSum;
  95. if (Coff!=0) {
  96. Crow++;
  97. }
  98. float width=(SCREEN_WIDTH)/1.0f;
  99. flowLayout.itemSize = CGSizeMake((width-4)/CrowItemSum, SCREEN_WIDTH/4+offHeight2);
  100. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, width, (SCREEN_WIDTH/4+offHeight2)*Crow) collectionViewLayout:flowLayout];
  101. _collectionView.layer.masksToBounds=YES;
  102. _collectionView.backgroundColor=[UIColor whiteColor];
  103. _collectionView.delegate = self;
  104. _collectionView.dataSource = self;
  105. [_collectionView registerNib:[UINib nibWithNibName:@"LDMorePicCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"picCollectionViewCell"];
  106. }
  107. return _collectionView;
  108. }
  109. @end