// // PBImageTableViewCell.m // IphoneBIMe // // Created by zjf on 2019/1/10. // Copyright © 2019 ProBIM. All rights reserved. // #import "PBIssueImageTableViewCell.h" #import "PBImageCollectionViewCell.h" static NSString *const ImageCellID = @"ImageCellID"; @interface PBIssueImageTableViewCell () @property (nonatomic, strong) UICollectionView *collectionView; @end @implementation PBIssueImageTableViewCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { [self setupUI]; } return self; } - (void)setupUI { self.contentView.backgroundColor = [UIColor yellowColor]; UILabel *titleL = [UILabel z_labelWithText:@"关联图片(选填)" Color:TitleColor isBold:NO Font:TitleFontSize]; [self.contentView addSubview:titleL]; [titleL mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.contentView).offset(13); make.left.equalTo(self.contentView).offset(16); make.height.equalTo(@22); make.width.equalTo(@129); }]; UIButton *selectBtn = [UIButton z_textButton:@"请选择" fontSize:MarkedFontSize normalColor:PromptColor]; [selectBtn addTarget:self action:@selector(chooseImageAction:) forControlEvents:UIControlEventTouchUpInside]; [self.contentView addSubview:selectBtn]; [selectBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.contentView).offset(-16); make.centerY.equalTo(titleL); }]; UIView *lineV = [[UIView alloc] init]; [self.contentView addSubview:lineV]; [lineV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(titleL.mas_bottom).offset(16); make.left.equalTo(self.contentView).offset(20); make.right.equalTo(self.contentView).offset(-20); make.height.equalTo(@1); }]; UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout.alloc init]; layout.itemSize = CGSizeMake(80, 80); layout.minimumLineSpacing = 10; layout.scrollDirection = UICollectionViewScrollDirectionVertical; self.collectionView = [UICollectionView.alloc initWithFrame:CGRectZero collectionViewLayout:layout]; [self.collectionView registerClass:[PBImageCollectionViewCell class] forCellWithReuseIdentifier:ImageCellID]; self.collectionView.delegate = self; self.collectionView.dataSource = self; self.collectionView.bounces = NO; self.collectionView.showsHorizontalScrollIndicator = NO; self.collectionView.backgroundColor = [UIColor whiteColor]; [self.contentView addSubview:self.collectionView]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(lineV.mas_bottom).offset(16); make.left.equalTo(self.contentView).offset(33); make.right.equalTo(self.contentView).offset(-32); make.height.equalTo(@80); make.bottom.equalTo(self.contentView).offset(-26); }]; } - (void)chooseImageAction:(UIButton *)button { if (self.ChooseImageBlock) { self.ChooseImageBlock(); } } #pragma mark - UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { // return self.imageArr.count + 1; return 0; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { PBImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ImageCellID forIndexPath:indexPath]; cell.image = self.imageArr[indexPath.row]; cell.DeleteImageBlock = ^{ [self.imageArr removeObjectAtIndex:indexPath.row]; [self.collectionView reloadData]; }; return cell; } #pragma mark - UICollectionViewDelegate - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { SDPhotoBrowser *photoBrowser = [SDPhotoBrowser new]; photoBrowser.delegate = self; photoBrowser.currentImageIndex = indexPath.item; photoBrowser.imageCount = self.imageArr.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]]; return cell.image; } - (void)setImageArr:(NSMutableArray *)imageArr { _imageArr = imageArr; [self.collectionView reloadData]; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end