XYPersonalContentNormalItemsTableViewCell.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // XYPersonalContentNormalItemsTableViewCell.m
  3. // TianYou
  4. //
  5. // Created by 翟玉磊 on 2021/7/21.
  6. //
  7. #import "XYPersonalContentNormalItemsTableViewCell.h"
  8. #import "XYPersonalContentNormalItemCollectionViewCell.h"
  9. #import "XYPersonalCenterListModel.h"
  10. @interface XYPersonalContentNormalItemsTableViewCell ()<UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
  11. @property (nonatomic, strong) UICollectionView *collectionView;
  12. @property (nonatomic, strong) NSMutableArray *dataSource;
  13. @property (nonatomic, strong) NSIndexPath *indexPath;
  14. @end
  15. @implementation XYPersonalContentNormalItemsTableViewCell
  16. + (instancetype)cellWithTableView:(UITableView *)tableView {
  17. XYPersonalContentNormalItemsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:XYPersonalContentNormalItemsTableViewCell.className];
  18. if (cell == nil) {
  19. cell = [[XYPersonalContentNormalItemsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:XYPersonalContentNormalItemsTableViewCell.className];
  20. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  21. }
  22. return cell;
  23. }
  24. - (void)configureModel:(XYPersonalCenterListModel *)model {
  25. self.dataSource = [NSMutableArray arrayWithArray:model.urlItemArray];
  26. [self.collectionView reloadData];
  27. }
  28. - (void)setIndexPath:(NSIndexPath *)indexPath rowsInSection:(NSInteger)rows {
  29. self.indexPath = indexPath;
  30. }
  31. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  32. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  33. [self setupUI];
  34. }
  35. return self;
  36. }
  37. - (void)setupUI {
  38. self.contentView.backgroundColor = Color_White;
  39. [self.contentView addSubview:self.collectionView];
  40. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  41. make.left.right.equalTo(self.contentView);
  42. make.top.equalTo(self.contentView).offset(8.0f);
  43. make.bottom.equalTo(self.contentView).offset(-8.0f);
  44. }];
  45. }
  46. #pragma mark — UICollectionViewDelegateFlowLayout, UICollectionViewDataSource
  47. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  48. return self.dataSource.count;
  49. }
  50. // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
  51. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  52. XYPersonalContentNormalItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:XYPersonalContentNormalItemCollectionViewCell.className forIndexPath:indexPath];
  53. [cell configureModel:self.dataSource[indexPath.row]];
  54. return cell;
  55. }
  56. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  57. [collectionView deselectItemAtIndexPath:indexPath animated:YES];
  58. if (self.delegate && [self.delegate respondsToSelector:@selector(normalItemsSelectedWithListIndexPath:itemIndexPath:)]) {
  59. [self.delegate normalItemsSelectedWithListIndexPath:self.indexPath itemIndexPath:indexPath];
  60. }
  61. }
  62. /**
  63. #import "XYPersonalCenterHeaderEditAlertView.h"
  64. */
  65. #pragma mark — Getter
  66. - (UICollectionView *)collectionView {
  67. if (!_collectionView) {
  68. UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
  69. flowLayout.itemSize = CGSizeMake(/*67.0f*/(kScreenWidth-30)/4.0, 64.0f);
  70. flowLayout.minimumLineSpacing = 2.0f;
  71. flowLayout.minimumInteritemSpacing = 0.0f;
  72. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
  73. _collectionView.delegate = self;
  74. _collectionView.dataSource = self;
  75. _collectionView.backgroundColor = Color_Clear;
  76. _collectionView.scrollEnabled = NO;
  77. [_collectionView registerCell:XYPersonalContentNormalItemCollectionViewCell.className];
  78. }
  79. return _collectionView;
  80. }
  81. @end