口袋优选

KBMorePicCollectionCell.m 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // KBMorePicCollectionCell.m
  3. // YouHuiProject
  4. //
  5. // Created by jcymac on 2018/4/24.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBMorePicCollectionCell.h"
  9. #import "KBMorePicCollectionViewCell.h"
  10. #import "KBMorePicCollectionModel.h"
  11. @interface KBMorePicCollectionCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  12. @property(nonatomic,strong)UICollectionView *collectionView;
  13. @property (nonatomic, strong) NSMutableArray *dataArr;
  14. @end
  15. @implementation KBMorePicCollectionCell
  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. KBMorePicCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"picCollectionViewCell" forIndexPath:indexPath];
  50. if (!cell) {
  51. }
  52. KBMorePicCollectionModel *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. flowLayout.minimumLineSpacing = 0;
  73. flowLayout.minimumInteritemSpacing = 0;
  74. if (sumCell==0) {
  75. sumCell=4;
  76. }
  77. NSInteger offHeight2=0;
  78. if (iPhone5) {
  79. offHeight2=10;
  80. }
  81. NSInteger Crow=1;
  82. NSInteger Coff=-1;
  83. NSInteger CrowItemSum=5;
  84. Coff=sumCell%CrowItemSum;
  85. if (Coff!=0) {
  86. CrowItemSum=4;
  87. Coff=sumCell%CrowItemSum;
  88. if (Coff!=0) {
  89. Coff=sumCell%3;
  90. CrowItemSum=3;
  91. if (Coff!=0) {
  92. CrowItemSum=5;
  93. }
  94. }
  95. }
  96. Crow=sumCell/CrowItemSum;
  97. if (Coff!=0) {
  98. Crow++;
  99. }
  100. float width=(SCREEN_WIDTH-FITSIZE(20))/1.0f;
  101. flowLayout.itemSize = CGSizeMake((width-4)/CrowItemSum, SCREEN_WIDTH/4+offHeight2);
  102. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(FITSIZE(10), 0, width, (SCREEN_WIDTH/4+offHeight2)*Crow) collectionViewLayout:flowLayout];
  103. _collectionView.layer.cornerRadius=8;
  104. _collectionView.layer.masksToBounds=YES;
  105. _collectionView.backgroundColor=[UIColor whiteColor];
  106. _collectionView.delegate = self;
  107. _collectionView.dataSource = self;
  108. [_collectionView registerNib:[UINib nibWithNibName:@"KBMorePicCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"picCollectionViewCell"];
  109. }
  110. return _collectionView;
  111. }
  112. @end