STTablePhotoCell.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // STTablePhotoCell.m
  3. // BuguLive
  4. //
  5. // Created by 岳克奎 on 17/4/18.
  6. // Copyright © 2017年 xfg. All rights reserved.
  7. //
  8. #import "STTablePhotoCell.h"
  9. #import "STCollectionPhotoCell.h"
  10. @implementation STTablePhotoCell
  11. - (void)awakeFromNib {
  12. [super awakeFromNib];
  13. // Initialization code
  14. self.collectionView.delegate = self;
  15. self.collectionView.dataSource = self;
  16. [self.collectionView registerNib:[UINib nibWithNibName:@"STCollectionPhotoCell"
  17. bundle:nil]
  18. forCellWithReuseIdentifier:@"STCollectionPhotoCell"];
  19. self.collectionView.scrollEnabled = NO;
  20. self.selectedMArray = @[].mutableCopy;
  21. self.recordIndexRow = 1000000;
  22. }
  23. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  24. [super setSelected:selected animated:animated];
  25. // Configure the view for the selected state
  26. }
  27. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  28. if (self.stTablePhotoCellType == STTablePhotoUnSelected ||self.stTablePhotoCellType == STTablePhotoSelectCover||self.stTablePhotoCellType == STTablePhotoShow) {
  29. return _dataSoureMArray.count;
  30. }
  31. if (_dataSoureMArray.count>8) {
  32. return 9;
  33. }
  34. return _dataSoureMArray.count;
  35. }
  36. #pragma mark ---- cell
  37. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  38. STCollectionPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"STCollectionPhotoCell"
  39. forIndexPath:indexPath];
  40. [cell.bgImgView setImage:_dataSoureMArray[indexPath.row]];
  41. //带删除的
  42. if (self.stTablePhotoCellType == STTablePhotoDelete) {
  43. if (indexPath.row+1 == self.dataSoureMArray.count ) {
  44. cell.selectStateImgView.hidden = YES;
  45. }else{
  46. cell.selectStateImgView.hidden = NO;
  47. }
  48. [cell.selectStateImgView setImage:[UIImage imageNamed:@"st_delete_photo"]];
  49. if (indexPath.row+1 == self.dataSoureMArray.count ) {
  50. cell.selectStateImgView.hidden = YES;
  51. }else{
  52. cell.selectStateImgView.hidden = NO;
  53. }
  54. }
  55. if (self.stTablePhotoCellType == STTablePhotoUnSelected) {
  56. cell.selectStateImgView.hidden = NO;
  57. [cell.selectStateImgView setImage:[UIImage imageNamed:@"st_photosDynamic_unselect"]];
  58. }
  59. //设置封面cell
  60. if (self.stTablePhotoCellType == STTablePhotoSelectCover) {
  61. cell.bgImgView.hidden = NO;
  62. [cell.bgImgView setImage:_dataSoureMArray[indexPath.row]];
  63. cell.selectCoverImgView.hidden = NO;
  64. [cell.selectCoverImgView setImage:[UIImage imageNamed:@"st_photosDynamic_cover_unselect"]];
  65. }
  66. return cell;
  67. }
  68. //点击cell
  69. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  70. //选择类型cell
  71. if (self.stTablePhotoCellType == STTablePhotoUnSelected) {
  72. UIImage *image = self.dataSoureMArray[indexPath.row];
  73. //把数据传走
  74. STCollectionPhotoCell *cell =(STCollectionPhotoCell *)[collectionView cellForItemAtIndexPath:indexPath];
  75. cell.isSelectedState = !cell.isSelectedState;
  76. [cell.selectStateImgView setImage:[UIImage imageNamed:cell.isSelectedState?@"st_photosDynamic_select":@"st_photosDynamic_unselect"]];
  77. //选择
  78. if ( cell.isSelectedState) {
  79. //放入数组
  80. if ([self.selectedMArray containsObject:image]) {
  81. return;
  82. }else{
  83. [self.selectedMArray addObject:image];
  84. }
  85. }
  86. //未选择
  87. else{
  88. if ([self.selectedMArray containsObject:image]) {
  89. [self.selectedMArray removeObject:image];
  90. }else{
  91. return;
  92. }
  93. }
  94. [cell setNeedsDisplay];
  95. }
  96. if (self.stTablePhotoCellType == STTablePhotoSelectCover) {
  97. if (self.recordIndexRow> _dataSoureMArray.count) {
  98. }else{
  99. NSIndexPath *oldIndexPath=[NSIndexPath indexPathForRow:_recordIndexRow inSection:0];
  100. STCollectionPhotoCell *oldCell =(STCollectionPhotoCell *)[collectionView cellForItemAtIndexPath:oldIndexPath];
  101. [oldCell.selectCoverImgView setImage:[UIImage imageNamed:@"st_photosDynamic_cover_unselect"]];
  102. [oldCell setNeedsDisplay];
  103. }
  104. STCollectionPhotoCell *cell =(STCollectionPhotoCell *)[collectionView cellForItemAtIndexPath:indexPath];
  105. [cell.selectCoverImgView setImage:[UIImage imageNamed:@"st_photosDynamic_cover_select"]];
  106. [cell setNeedsDisplay];
  107. self.recordIndexRow = indexPath.row;
  108. }
  109. //代理传数据走
  110. if(_delegate &&[_delegate respondsToSelector:@selector(showSTTablePhotoCell:andDidSelectItemAtIndexPath:)]){
  111. [_delegate showSTTablePhotoCell:self
  112. andDidSelectItemAtIndexPath:indexPath];
  113. }
  114. }
  115. -(void)setDelegate:(id<STTablePhotoCellDelegate>)delegate{
  116. _delegate = delegate;
  117. }
  118. -(void)setDataSoureMArray:(NSMutableArray *)dataSoureMArray{
  119. if (_dataSoureMArray.count>0) {
  120. [_dataSoureMArray removeAllObjects];
  121. }
  122. _dataSoureMArray = dataSoureMArray.mutableCopy;
  123. [_collectionView reloadData];
  124. }
  125. @end