RAPhotoCell.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // RAPhotoCell.m
  3. // Apex And Drivers
  4. //
  5. // Created by Jack on 2018/10/30.
  6. // Copyright © 2018年 USAI. All rights reserved.
  7. //
  8. #import "RAPhotoCell.h"
  9. #import "RAPhotoItemCell.h"
  10. #import "RAPhotoModel.h"
  11. #import "RAPhotoModelDelegate.h"
  12. #import "RAPhotoLayout.h"
  13. @interface RAPhotoCell ()<UICollectionViewDelegate,UICollectionViewDataSource,RAPhotoModelDelegate>
  14. @property (nonatomic,strong) IBOutlet UILabel *titleLabel;
  15. @property (nonatomic,strong) IBOutlet UILabel *requiredLabel;
  16. @property (nonatomic,strong) IBOutlet UICollectionView *photoCollectionView;
  17. @property (nonatomic,strong) RAPhotoLayout *photoLayout;
  18. @end
  19. @implementation RAPhotoCell
  20. + (NSString *)reuseId {
  21. return @"RAPhotoCell";
  22. }
  23. + (void)regist2TableView:(UITableView *)tableView {
  24. if (tableView == nil) {
  25. return;
  26. }
  27. [tableView registerNib:[UINib nibWithNibName:@"RAPhotoCell" bundle:nil] forCellReuseIdentifier:self.reuseId];
  28. }
  29. #pragma mark - Super
  30. - (void)awakeFromNib {
  31. [super awakeFromNib];
  32. // Initialization code
  33. [RAPhotoItemCell regist2CollectionView:self.photoCollectionView];
  34. self.photoCollectionView.collectionViewLayout = self.photoLayout;
  35. }
  36. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  37. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  38. }
  39. return self;
  40. }
  41. - (void)prepareForReuse {
  42. [super prepareForReuse];
  43. self.model = nil;
  44. }
  45. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  46. [super setSelected:selected animated:animated];
  47. // Configure the view for the selected state
  48. }
  49. #pragma mark - Setter
  50. - (void)setModel:(RAPhotoModel *)model {
  51. // NSLog(@"CollectionCell %p %p %p",self,_model,model);
  52. _model.delegate = nil;
  53. _model = model;
  54. _model.delegate = self;
  55. [self refreshUI];
  56. }
  57. #pragma mark - Getter
  58. - (RAPhotoLayout *)photoLayout {
  59. if (!_photoLayout) {
  60. _photoLayout = [[RAPhotoLayout alloc] init];
  61. }
  62. return _photoLayout;
  63. }
  64. #pragma mark - Model Delegate
  65. - (void)refreshUI {
  66. // NSLog(@"CollectionCell refreshUI %p %p",self,_model);
  67. NSString *title = _model.title;
  68. BOOL required = _model.required;
  69. self.titleLabel.text = title;
  70. self.requiredLabel.hidden = !required;
  71. self.photoLayout.delegate = _model;
  72. [self.photoCollectionView reloadData];
  73. }
  74. - (void)unbind {
  75. _model = nil;
  76. // [self refreshUI];
  77. }
  78. #pragma mark - CollectionView DataSource
  79. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  80. return 1;
  81. }
  82. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  83. NSInteger count = self.model.photos.count;
  84. return count;
  85. }
  86. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  87. RAPhotoItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:RAPhotoItemCell.reuseId forIndexPath:indexPath];
  88. RAPhotoItemModel *model = [self.model.photos objectAtIndex:indexPath.row];
  89. cell.model = model;
  90. __weak typeof(self) weakSelf = self;
  91. cell.clickBlk = ^(RAPhotoItemModel *model) {
  92. if (weakSelf) {
  93. __strong typeof(weakSelf) strongSelf = weakSelf;
  94. [strongSelf clickItem:model];
  95. }
  96. };
  97. return cell;
  98. }
  99. #pragma mark - CollectionView Delegate
  100. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  101. [self clickItem:[self.model photoItemAtIndex:indexPath.item]];
  102. }
  103. #pragma mark - Action
  104. - (void)clickItem:(RAPhotoItemModel *)model {
  105. if (self.delegate && [self.delegate respondsToSelector:@selector(photoCell:didClickPhotoItem:)]) {
  106. [self.delegate photoCell:self didClickPhotoItem:model];
  107. }
  108. }
  109. @end