// // PBRecordImageTableViewCell.m // IphoneBIMe // // Created by zjf on 2020/12/29. // Copyright © 2020 ProBIM. All rights reserved. // #import "PBRecordImageTableViewCell.h" #import "PBRecordModel.h" #import "PBImageCollectionViewCell.h" #import "PBVideoCollectionViewCell.h" static NSString *const ImageCellID = @"ImageCellID"; static NSString *const VideoCellID = @"VideoCellID"; @interface PBRecordImageTableViewCell() @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, weak) UILabel *timeL; @end @implementation PBRecordImageTableViewCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { [self setupUI]; } return self; } - (void)setupUI { self.contentView.backgroundColor = PBColor(244, 245, 246); UIView *bgV = [[UIView alloc] init]; bgV.backgroundColor = [UIColor whiteColor]; [self.contentView addSubview:bgV]; [bgV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.left.right.bottom.equalTo(self.contentView); }]; UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout.alloc init]; layout.itemSize = CGSizeMake(80, 80); layout.minimumLineSpacing = 10; layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; self.collectionView = [UICollectionView.alloc initWithFrame:CGRectZero collectionViewLayout:layout]; [self.collectionView registerClass:[PBImageCollectionViewCell class] forCellWithReuseIdentifier:ImageCellID]; [self.collectionView registerClass:[PBVideoCollectionViewCell class] forCellWithReuseIdentifier:VideoCellID]; self.collectionView.delegate = self; self.collectionView.dataSource = self; self.collectionView.bounces = NO; self.collectionView.scrollEnabled = NO; self.collectionView.showsHorizontalScrollIndicator = NO; self.collectionView.backgroundColor = [UIColor whiteColor]; [self.contentView addSubview:self.collectionView]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(bgV).offset(10); make.left.equalTo(self.contentView).offset(10); make.right.equalTo(self.contentView).offset(-10); make.height.equalTo(@80); }]; // [self.collectionView circleViewWithRadius:6]; UILabel *timeL = [UILabel z_labelWithText:@"" Color:PBColor(166, 174, 182) isBold:YES Font:13]; [self.contentView addSubview:timeL]; [timeL mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.collectionView.mas_bottom).offset(10); make.left.equalTo(bgV).offset(10); make.height.equalTo(@16); make.bottom.equalTo(bgV).offset(-10); }]; self.timeL = timeL; } #pragma mark - UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.recordModel.Attachments.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *dict = self.recordModel.Attachments[indexPath.row]; if ([[dict valueForKey:@"AttachmentType"] isEqualToString:@".mp4"]) { PBVideoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:VideoCellID forIndexPath:indexPath]; NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,[dict valueForKey:@"AttachmentUrl"]]; cell.videoUrl = url; cell.PlayVideoBlock = ^{ if (self.PlayVideoBlock) { self.PlayVideoBlock(url); } }; return cell; }else { PBImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ImageCellID forIndexPath:indexPath]; NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,[dict valueForKey:@"AttachmentUrl"]]; cell.roleType = PARTICIPANT; if (self.recordModel.Attachments.count > 3 & indexPath.item == 2) { cell.countStr = [NSString stringWithFormat:@"%zd", self.recordModel.Attachments.count]; }else { cell.countStr = nil; } cell.imageUrl = url; return cell; } } #pragma mark - UICollectionViewDelegate - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *dict = self.recordModel.Attachments[indexPath.row]; if ([[dict valueForKey:@"AttachmentType"] isEqualToString:@".mp4"]) { if (self.PlayVideoBlock) { self.PlayVideoBlock([NSString stringWithFormat:@"%@%@",BaseUrl, [dict valueForKey:@"AttachmentUrl"]]); } }else { SDPhotoBrowser *photoBrowser = [SDPhotoBrowser new]; photoBrowser.delegate = self; photoBrowser.currentImageIndex = indexPath.item; photoBrowser.imageCount = self.recordModel.Attachments.count; photoBrowser.sourceImagesContainerView = self.collectionView; [photoBrowser show]; } } #pragma mark SDPhotoBrowserDelegate // 返回临时占位图片(即原来的小图) - (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index { // 不建议用此种方式获取小图,这里只是为了简单实现展示而已 PBImageCollectionViewCell *cell = (PBImageCollectionViewCell *)[self collectionView:self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]]; NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:cell.imageUrl]]; UIImage *image = [UIImage imageWithData:data];; return image; } - (void)setRecordModel:(PBRecordModel *)recordModel { _recordModel = recordModel; self.timeL.text = [recordModel.CreateDate stringByReplacingOccurrencesOfString:@"T" withString:@" "]; UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout.alloc init]; layout.itemSize = CGSizeMake(1, 1); layout.minimumLineSpacing = 0; layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; CGFloat height; NSInteger count = recordModel.Attachments.count; if (count <= 0) { height = 0.f; }else if (count == 1) { height = 184.f; layout.itemSize = CGSizeMake(MainScreenWidth - 20 * 2, 184); layout.minimumLineSpacing = 0.f; }else if (count == 2) { height = 184.f; layout.itemSize = CGSizeMake((MainScreenWidth - (20 * 2) - 1) / 2, 184); layout.minimumLineSpacing = 1.f; }else { height = 92.f; layout.itemSize = CGSizeMake((MainScreenWidth - (20 + 3) * 2) / 3, 92); layout.minimumLineSpacing = 3.f; } [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) { make.height.equalTo(@(height)); }]; if (count > 0) { [self.collectionView setCollectionViewLayout:layout animated:NO]; } [self.collectionView reloadData]; } - (void)awakeFromNib { [super awakeFromNib]; // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end