// // RAPhotoCell.m // Apex And Drivers // // Created by Jack on 2018/10/30. // Copyright © 2018年 USAI. All rights reserved. // #import "RAPhotoCell.h" #import "RAPhotoItemCell.h" #import "RAPhotoModel.h" #import "RAPhotoModelDelegate.h" #import "RAPhotoLayout.h" @interface RAPhotoCell () @property (nonatomic,strong) IBOutlet UILabel *titleLabel; @property (nonatomic,strong) IBOutlet UILabel *requiredLabel; @property (nonatomic,strong) IBOutlet UICollectionView *photoCollectionView; @property (nonatomic,strong) RAPhotoLayout *photoLayout; @end @implementation RAPhotoCell + (NSString *)reuseId { return @"RAPhotoCell"; } + (void)regist2TableView:(UITableView *)tableView { if (tableView == nil) { return; } [tableView registerNib:[UINib nibWithNibName:@"RAPhotoCell" bundle:nil] forCellReuseIdentifier:self.reuseId]; } #pragma mark - Super - (void)awakeFromNib { [super awakeFromNib]; // Initialization code [RAPhotoItemCell regist2CollectionView:self.photoCollectionView]; self.photoCollectionView.collectionViewLayout = self.photoLayout; } - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { } return self; } - (void)prepareForReuse { [super prepareForReuse]; self.model = nil; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } #pragma mark - Setter - (void)setModel:(RAPhotoModel *)model { // NSLog(@"CollectionCell %p %p %p",self,_model,model); _model.delegate = nil; _model = model; _model.delegate = self; [self refreshUI]; } #pragma mark - Getter - (RAPhotoLayout *)photoLayout { if (!_photoLayout) { _photoLayout = [[RAPhotoLayout alloc] init]; } return _photoLayout; } #pragma mark - Model Delegate - (void)refreshUI { // NSLog(@"CollectionCell refreshUI %p %p",self,_model); NSString *title = _model.title; BOOL required = _model.required; self.titleLabel.text = title; self.requiredLabel.hidden = !required; self.photoLayout.delegate = _model; [self.photoCollectionView reloadData]; } - (void)unbind { _model = nil; // [self refreshUI]; } #pragma mark - CollectionView DataSource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { NSInteger count = self.model.photos.count; return count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { RAPhotoItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:RAPhotoItemCell.reuseId forIndexPath:indexPath]; RAPhotoItemModel *model = [self.model.photos objectAtIndex:indexPath.row]; cell.model = model; __weak typeof(self) weakSelf = self; cell.clickBlk = ^(RAPhotoItemModel *model) { if (weakSelf) { __strong typeof(weakSelf) strongSelf = weakSelf; [strongSelf clickItem:model]; } }; return cell; } #pragma mark - CollectionView Delegate - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { [self clickItem:[self.model photoItemAtIndex:indexPath.item]]; } #pragma mark - Action - (void)clickItem:(RAPhotoItemModel *)model { if (self.delegate && [self.delegate respondsToSelector:@selector(photoCell:didClickPhotoItem:)]) { [self.delegate photoCell:self didClickPhotoItem:model]; } } @end