ImageAttachmentCell.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //
  2. // ImageAttachmentCell.m
  3. // IBOSS
  4. //
  5. // Created by 关宏厚 on 2020/7/27.
  6. // Copyright © 2020 elongtian. All rights reserved.
  7. //
  8. #import "ImageAttachmentCell.h"
  9. #import "AlbumPhotoCollectionViewCell.h"
  10. #import "XHAlbumCollectionViewFlowLayout.h"
  11. #import "XHImageViewer.h"
  12. #import "ImageModel.h"
  13. #define PhotoCollectionViewCellIdentifier @"PhotoCollectionViewCellIdentifier"
  14. @interface AttachmentPhotoCollectionView : UICollectionView
  15. @end
  16. @implementation AttachmentPhotoCollectionView
  17. @end
  18. @interface ImageAttachmentCell(){
  19. // 底部灰背景
  20. UIView *_viewBottomBackgroud;
  21. // UIControl控件
  22. UIControl *_control;
  23. }
  24. /**
  25. 图片控件
  26. */
  27. @property (nonatomic, strong) AttachmentPhotoCollectionView *photoCollectionView;
  28. @end
  29. @implementation ImageAttachmentCell
  30. @synthesize subView;
  31. /**
  32. 初始化
  33. @param style <#style description#>
  34. @param reuseIdentifier <#reuseIdentifier description#>
  35. @return <#return value description#>
  36. */
  37. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  38. {
  39. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  40. if (self) {
  41. // Initialization code
  42. [self initUI];
  43. }
  44. return self;
  45. }
  46. /**
  47. 初始化ui
  48. */
  49. - (void)initUI
  50. {
  51. _control = [[UIControl alloc] initWithFrame:CGRectMake(0, 0,Screen_Width,40)];
  52. [_control addTarget:self action:@selector(headerClick:) forControlEvents:UIControlEventTouchUpInside];
  53. [self.contentView addSubview:_control];
  54. _imgInfoTitle = [[UILabel alloc] init];
  55. _imgInfoTitle.frame=CGRectMake(20,7,120,25);
  56. _imgInfoTitle.text = @"方案附件信息";
  57. _imgInfoTitle.backgroundColor = [UIColor clearColor];
  58. _imgInfoTitle.font = [UIFont systemFontOfSize:14.0f];
  59. _imgInfoTitle.textColor = [UIColor redColor];
  60. [self.contentView addSubview:_imgInfoTitle];
  61. _btnArrow = [UIButton buttonWithType:UIButtonTypeCustom];
  62. _btnArrow.frame=CGRectMake(Screen_Width-30,12,16, 16);
  63. [_btnArrow setBackgroundImage:[UIImage imageNamed:@"arrow-down"] forState: UIControlStateNormal];
  64. [_btnArrow addTarget:self action:@selector(headerClick:) forControlEvents:UIControlEventTouchUpInside];
  65. [self.contentView addSubview:_btnArrow];
  66. }
  67. /**
  68. 图片
  69. @param infoDetail <#infoDetail description#>
  70. */
  71. - (void)setImageInfo:(CustomerInfoDetailModel *)infoDetail {
  72. _infoDetailModel=infoDetail;
  73. NSMutableArray *imgArr;
  74. subView = [[UIView alloc]init];
  75. [self.contentView addSubview:subView];
  76. subView.backgroundColor = [UIColor clearColor];
  77. CGRect photoFrame;
  78. imgArr = [_infoDetailModel imageAttachmentDisplayArr];
  79. if(imgArr != nil&&[imgArr count]>0){
  80. photoFrame= [infoDetail getPhotoFrame:[infoDetail imageAttachmentDisplayArr ]];
  81. subView.frame=CGRectMake(0,CGRectGetMaxY(_control.frame), Screen_Width, [infoDetail getPhotoCollectionViewHeightWithPhotos:[infoDetail imageAttachmentDisplayArr ]]);
  82. //分割线
  83. UIView *_viewBackgroud = [UIView new];
  84. _viewBackgroud.frame = CGRectMake(0, 0, Screen_Width, 1);
  85. _viewBackgroud.backgroundColor = LineBackgroundColor;
  86. [subView addSubview:_viewBackgroud];
  87. _photoCollectionView = [[AttachmentPhotoCollectionView alloc] initWithFrame:photoFrame collectionViewLayout:[[XHAlbumCollectionViewFlowLayout alloc] init]];
  88. _photoCollectionView.backgroundColor = [UIColor clearColor];
  89. [_photoCollectionView registerClass:[AlbumPhotoCollectionViewCell class] forCellWithReuseIdentifier:PhotoCollectionViewCellIdentifier];
  90. _photoCollectionView.delegate = self;
  91. _photoCollectionView.scrollEnabled=NO;
  92. _photoCollectionView.dataSource = self;
  93. [subView addSubview:self.photoCollectionView];
  94. [self.photoCollectionView reloadData];
  95. }
  96. else
  97. {
  98. subView.frame=CGRectMake(0,CGRectGetMaxY(_control.frame), Screen_Width, 0);
  99. }
  100. _viewBottomBackgroud = [UIView new];
  101. _viewBottomBackgroud.backgroundColor = LineBackgroundColor;
  102. [self.contentView addSubview:_viewBottomBackgroud];
  103. if([_isOpen isEqualToString:@"1"])
  104. {
  105. subView.hidden=NO;
  106. _viewBottomBackgroud.frame = CGRectMake(0, CGRectGetMaxY(subView.frame), Screen_Width, 10);
  107. }
  108. else
  109. {
  110. subView.hidden=YES;
  111. _viewBottomBackgroud.frame = CGRectMake(0, CGRectGetMaxY(_control.frame), Screen_Width, 10);
  112. }
  113. }
  114. /**
  115. 图片个数
  116. @param collectionView <#collectionView description#>
  117. @param section <#section description#>
  118. @return <#return value description#>
  119. */
  120. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  121. return [[_infoDetailModel imageAttachmentDisplayArr ] count];
  122. }
  123. /**
  124. 单元格图片
  125. @param collectionView <#collectionView description#>
  126. @param indexPath <#indexPath description#>
  127. @return <#return value description#>
  128. */
  129. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  130. AlbumPhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:PhotoCollectionViewCellIdentifier forIndexPath:indexPath];
  131. cell.indexPath = indexPath;
  132. ImageModel *image = [[_infoDetailModel imageAttachmentDisplayArr ] objectAtIndex:indexPath.row];
  133. NSString *imagepath = [image imageName];
  134. if(imagepath != nil&&[imagepath length]>0){
  135. NSString *imageUrl = [NSString stringWithFormat:@"http://%@:%@/WebService/%@",kkServerUrl,kkServerPort,imagepath];
  136. NSURL *url = [NSURL URLWithString:imageUrl];
  137. [cell.photoImageView downloadImageWithURL:url];
  138. }
  139. return cell;
  140. }
  141. /**
  142. 点击事件
  143. @param sender <#sender description#>
  144. */
  145. - (void)headerClick:(id)sender{
  146. [self rotateArrowImage];
  147. if([self.delegate respondsToSelector:@selector(refreshTableView)])
  148. {
  149. [self.delegate performSelector:@selector(refreshTableView) withObject:nil afterDelay:0.0 ];
  150. }
  151. }
  152. /**
  153. 图片高度
  154. @param photos <#photos description#>
  155. @return <#return value description#>
  156. */
  157. + (CGFloat)getPhotoCollectionViewHeightWithPhotos:(NSArray *)photos {
  158. // 上下间隔已经在frame上做了
  159. NSInteger row = (photos.count / 3 + (photos.count % 3 ? 1 : 0));
  160. return (row *85 + ((row - 1) *5));
  161. }
  162. /**
  163. 展开,收缩
  164. */
  165. - (void)rotateArrowImage{
  166. //如果已经展开,收缩
  167. if([_isOpen isEqualToString:@"1"]){
  168. [UIView animateWithDuration:0.2 animations:^{
  169. self.btnArrow.transform = CGAffineTransformMakeRotation(0 / 180.0 * M_PI );
  170. }];
  171. _isOpen = @"0";
  172. subView.hidden=YES;
  173. _viewBottomBackgroud.frame = CGRectMake(0, CGRectGetMaxY(_control.frame), Screen_Width, 10);
  174. }else{
  175. [UIView animateWithDuration:0.2 animations:^{
  176. self.btnArrow.transform = CGAffineTransformMakeRotation(180 / 180.0 * M_PI );
  177. }];
  178. _isOpen = @"1";
  179. subView.hidden=NO;
  180. _viewBottomBackgroud.frame = CGRectMake(0, CGRectGetMaxY(subView.frame), Screen_Width, 10);
  181. }
  182. }
  183. /**
  184. 是否展开,收缩
  185. @param aIsOpen <#aIsOpen description#>
  186. */
  187. - (void)setIsOpen:(NSString *)aIsOpen{
  188. _isOpen = aIsOpen;
  189. if([_isOpen isEqualToString:@"1"]){
  190. self.btnArrow.transform = CGAffineTransformMakeRotation(180 / 180.0 * M_PI );
  191. subView.hidden=NO;
  192. _viewBottomBackgroud.frame = CGRectMake(0, CGRectGetMaxY(subView.frame), Screen_Width, 10);
  193. }
  194. else
  195. {
  196. self.btnArrow.transform = CGAffineTransformMakeRotation(0 / 180.0 * M_PI );
  197. subView.hidden=YES;
  198. _viewBottomBackgroud.frame = CGRectMake(0, CGRectGetMaxY(_control.frame), Screen_Width, 10);
  199. }
  200. }
  201. #pragma mark - UICollectionView Delegate
  202. /**
  203. 选择图片
  204. @param collectionView <#collectionView description#>
  205. @param indexPath <#indexPath description#>
  206. */
  207. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  208. [self showImageViewerAtIndexPath:indexPath];
  209. }
  210. /**
  211. 选择图片
  212. @param indexPath <#indexPath description#>
  213. */
  214. - (void)showImageViewerAtIndexPath:(NSIndexPath *)indexPath {
  215. AlbumPhotoCollectionViewCell *cell = (AlbumPhotoCollectionViewCell *)[self.photoCollectionView cellForItemAtIndexPath:indexPath];
  216. NSMutableArray *imageViews = [NSMutableArray array];
  217. NSArray *visibleCell = [self.photoCollectionView visibleCells];
  218. NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"indexPath" ascending:YES];
  219. visibleCell = [visibleCell sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
  220. [visibleCell enumerateObjectsUsingBlock:^(AlbumPhotoCollectionViewCell *cell, NSUInteger idx, BOOL *stop) {
  221. [imageViews addObject:[[cell.contentView subviews] lastObject]];
  222. }];
  223. XHImageViewer *imageViewer = [[XHImageViewer alloc] init];
  224. [imageViewer showWithImageViews:imageViews selectedView:[[cell.contentView subviews] lastObject]];
  225. }
  226. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  227. [super setSelected:selected animated:animated];
  228. // Configure the view for the selected state
  229. }
  230. @end