ContentViewCell.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // ContentViewCell.m
  3. // SheetViewDemo
  4. //
  5. // Created by Mengmin Duan on 2017/7/20.
  6. // Copyright © 2017年 Mengmin Duan. All rights reserved.
  7. //
  8. #import "ContentViewCell.h"
  9. @interface ContentViewCell () <UICollectionViewDelegate, UICollectionViewDataSource>
  10. @end
  11. @implementation ContentViewCell
  12. - (void)awakeFromNib {
  13. [super awakeFromNib];
  14. }
  15. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  16. [super setSelected:selected animated:animated];
  17. }
  18. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier;
  19. {
  20. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  21. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  22. layout.minimumLineSpacing = 0;
  23. layout.minimumInteritemSpacing = 0;
  24. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  25. self.cellCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:layout];
  26. self.cellCollectionView.showsHorizontalScrollIndicator = NO;
  27. self.cellCollectionView.bounces = NO;
  28. if (@available(iOS 11.0, *)) {
  29. self.cellCollectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
  30. } else {
  31. // Fallback on earlier versions
  32. }
  33. self.cellCollectionView.backgroundColor = [UIColor colorWithRed:(0x90 / 255.0)green:(0x90 / 255.0)blue:(0x90 / 255.0)alpha:1];
  34. self.cellCollectionView.layer.borderColor = [UIColor colorWithRed:(0x90 / 255.0)green:(0x90 / 255.0)blue:(0x90 / 255.0)alpha:1].CGColor;
  35. //self.cellCollectionView.layer.borderWidth = 1.0f;
  36. [self.cellCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"inner.cell"];
  37. self.cellCollectionView.dataSource = self;
  38. self.cellCollectionView.delegate = self;
  39. [self.contentView addSubview:self.cellCollectionView];
  40. }
  41. return self;
  42. }
  43. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  44. {
  45. if (self.contentViewCellDidScrollBlock) {
  46. self.contentViewCellDidScrollBlock(scrollView);
  47. }
  48. }
  49. #pragma mark -- UICollectionViewDelegate && UICollectionViewDataSource
  50. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  51. {
  52. return 1;
  53. }
  54. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  55. {
  56. return self.numberOfItemsBlock(section);
  57. }
  58. - (CGSize)collectionView:(UICollectionView *)uiCollectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  59. {
  60. return self.sizeForItemBlock(collectionViewLayout, indexPath);
  61. }
  62. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  63. {
  64. UICollectionViewCell *innerCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"inner.cell" forIndexPath:indexPath];
  65. if (innerCell) {
  66. for (UIView *view in innerCell.contentView.subviews) {
  67. [view removeFromSuperview];
  68. }
  69. }
  70. innerCell.layer.borderColor = [UIColor colorWithRed:(0x90 / 255.0)green:(0x90 / 255.0)blue:(0x90 / 255.0)alpha:1].CGColor;
  71. //innerCell.layer.borderWidth = 1;
  72. BOOL hasColor = NO;
  73. //if (self.cellWithColorBlock) hasColor = self.cellWithColorBlock(indexPath);
  74. innerCell.backgroundColor = (indexPath.row % 2 == 0 )?[UIColor colorWithRed:(248 / 255.0)green:(248 / 255.0)blue:(248 / 255.0)alpha:1]:[UIColor whiteColor];
  75. CGFloat width = self.sizeForItemBlock(nil, indexPath).width;
  76. CGFloat height = self.sizeForItemBlock(nil, indexPath).height;
  77. CGRect rect = CGRectMake(0, 0, width, height);
  78. UILabel *label = [[UILabel alloc] initWithFrame:rect];
  79. label.text = self.cellForItemBlock(indexPath);
  80. label.textColor = [UIColor colorWithRed:34/255 green:34/255 blue:34/255 alpha:1];
  81. label.font = [UIFont systemFontOfSize:11];
  82. label.textAlignment = NSTextAlignmentCenter;
  83. [innerCell.contentView addSubview:label];
  84. return innerCell;
  85. }
  86. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  87. {
  88. if (self.cellDidSelectBlock) {
  89. self.cellDidSelectBlock(indexPath);
  90. }
  91. }
  92. @end