Cell中实现多图展示

实现的效果如下:

 

主要实现代码如下:

  1 //
  2 //  DetailCell.m
  3 //  Floral
  4 //
  5 //  Created by 思 彭 on 16/9/21.
  6 //  Copyright © 2016年 思 彭. All rights reserved.
  7 //
  8 
  9 #import "DetailCell.h"
 10 #import "UIImageView+WebCache.h"
 11 #import "UIView+AdjustFrame.h"
 12 #import "ImageCollectionView.h"
 13 #import "ImageCell.h"
 14 
 15 #define K_SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
 16 #define K_SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
 17 
 18 @interface DetailCell ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
 19 {
 20     NSArray *imagesArray;  // 详情多张图片展示
 21     CGFloat imagesHeight;
 22 }
 23 /**
 24  *  头像
 25  */
 26 @property (weak, nonatomic) IBOutlet UIImageView *avaterImgView;
 27 /**
 28  *  用户名
 29  */
 30 @property (weak, nonatomic) IBOutlet UILabel *userNameLabel;
 31 /**
 32  *  描述
 33  */
 34 @property (weak, nonatomic) IBOutlet UILabel *contentLabel;
 35 /**
 36  *  时间
 37  */
 38 @property (weak, nonatomic) IBOutlet UILabel *timeLabel;
 39 /**
 40  *  更多
 41  */
 42 @property (weak, nonatomic) IBOutlet UIButton *moreButton;
 43 /**
 44  *  "更多"按钮被点击
 45  *
 46  *  @param sender <#sender description#>
 47  */
 48 - (IBAction)moreBUttonClick:(id)sender;
 49 /**
 50  *  collectionView
 51  */
 52 @property (weak, nonatomic) IBOutlet UIView *imagesView;
 53 /**
 54  *  赞
 55  */
 56 @property (weak, nonatomic) IBOutlet UIButton *loveButton;
 57 /**
 58  *  评论
 59  */
 60 @property (weak, nonatomic) IBOutlet UIButton *commentButton;
 61 /**
 62  *  分享
 63  */
 64 @property (weak, nonatomic) IBOutlet UIButton *shareButton;
 65 @property (weak, nonatomic) IBOutlet UILabel *morningLabel;
 66 @property (weak, nonatomic) IBOutlet NSLayoutConstraint *morningLabelTop;
 67 @property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomButtonY;
 68 
 69 @property (nonatomic,strong) ImageCollectionView *collectionView;
 70 
 71 @end
 72 
 73 @implementation DetailCell
 74 
 75 - (void)awakeFromNib {
 76 
 77     [super awakeFromNib];
 78     // Xcode 8必须自己设置,不然不显示
 79     self.avaterImgView.layer.cornerRadius = 25;
 80     self.avaterImgView.layer.masksToBounds = YES;
 81     self.userNameLabel.font = [UIFont systemFontOfSize:14];
 82     self.timeLabel.font = [UIFont systemFontOfSize:14];
 83     self.contentLabel.font = [UIFont systemFontOfSize:14];
 84     self.morningLabel.font = [UIFont systemFontOfSize:14];
 85     self.contentLabel.textColor = [UIColor grayColor];
 86     self.timeLabel.textColor = [UIColor grayColor];
 87     self.morningLabel.textColor = [UIColor grayColor];
 88     // 创建collectionView
 89     
 90     UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
 91     layout.itemSize = CGSizeMake((K_SCREEN_WIDTH - 15) / 2, (K_SCREEN_WIDTH - 15) / 2);
 92     layout.sectionInset = UIEdgeInsetsMake(5,5,5,5);
 93     layout.minimumLineSpacing = 5;
 94     layout.minimumInteritemSpacing = 0;
 95     self.collectionView = [[ImageCollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
 96     self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
 97     self.collectionView.dataSource = self;
 98     self.collectionView.delegate = self;
 99     self.collectionView.backgroundColor = [UIColor clearColor];
100     [self.imagesView addSubview:self.collectionView];
101     
102     // 注册cell
103     [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([ImageCell class]) bundle:nil] forCellWithReuseIdentifier:@"cell"];
104 }
105 
106 - (void)setModel:(TieZiListModel *)model{
107     
108     _model = model;
109     [self.avaterImgView sd_setImageWithURL:[NSURL URLWithString:self.model.customer.headImg] placeholderImage:[UIImage imageNamed:@"p_avatar"]];
110     self.userNameLabel.text = self.model.customer.userName;
111     if ([self.model.customer.content isEqualToString:@""]) {
112         
113         self.contentLabel.text = @"这家伙很懒,什么也没留下";
114     }
115     else{
116         self.contentLabel.text = self.model.content;
117     }
118     self.morningLabel.text = self.model.content;
119     [self.loveButton setTitle:[NSString stringWithFormat:@"%ld",self.model.read] forState:UIControlStateNormal];
120     [self.commentButton setTitle:[NSString stringWithFormat:@"%ld",self.model.comment] forState:UIControlStateNormal];
121     [self.shareButton setTitle:[NSString stringWithFormat:@"%ld",self.model.share] forState:UIControlStateNormal];
122     // 判空处理
123     if (self.model.createDate.length) {
124         
125         NSString *timeString = [self.model.createDate componentsSeparatedByString:@"."][0];
126         self.timeLabel.text = timeString;
127     }
128     [self.collectionView reloadData];
129     self.collectionView.frame = CGRectMake(0, self.imagesView.y - 70, self.imagesView.width, [self getCollectionViewHeight]);
130     self.model.cellHeight = 70 + 70 + [self getCollectionViewHeight];
131 }
132 
133 
134 /* 更多按钮被点击
135 
136  @param sender 点击的按钮
137  */
138 - (IBAction)moreBUttonClick:(id)sender {
139     
140     if ([self.delegate respondsToSelector:@selector(DidClickMoreButton:)]) {
141         [self.delegate DidClickMoreButton:self];
142     }
143 }
144 - (IBAction)zanButtonClick:(id)sender {
145     
146     [sender setImage:[UIImage imageNamed:@"p_zan-selected"] forState:UIControlStateSelected];
147 }
148 - (IBAction)commentClick:(id)sender {
149     if ([self.delegate respondsToSelector:@selector(DidClickCommentButton:)]) {
150         [self.delegate DidClickCommentButton:self];
151     }
152 }
153 - (IBAction)shareButtonClick:(id)sender {
154     
155     if ([self.delegate respondsToSelector:@selector(DidClickShareButton:)]) {
156         [self.delegate DidClickShareButton:self];
157     }
158 }
159 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
160     
161     NSString *attachment = self.model.attachment;
162     NSArray *imgArray = [attachment componentsSeparatedByString:@","];
163     return imgArray.count;
164 }
165 
166 - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
167     
168     ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
169     NSString *attachment = self.model.attachment;
170     cell.backgroundColor = [UIColor greenColor];
171     NSArray *imgArray = [attachment componentsSeparatedByString:@","];
172     cell.imageName = imgArray[indexPath.row];
173     return cell;
174 }
175 
176 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
177     
178     return UIEdgeInsetsMake(0,5,0,5);
179 }
180 
181 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
182     
183     return CGSizeMake((K_SCREEN_WIDTH - 15) / 2, (K_SCREEN_WIDTH - 15) / 2);
184 }
185 
186 - (CGFloat)getCollectionViewHeight{
187     
188     CGFloat collectionViewHeight = 0.0;
189     CGFloat height = 0.0;
190     NSString *attachment = self.model.attachment;
191     NSArray *imgArray = [attachment componentsSeparatedByString:@","];
192     if (imgArray.count % 2 != 0) {
193         
194         height = imgArray.count / 2 + 1;
195     }
196     else{
197         height = imgArray.count / 2;
198     }
199     collectionViewHeight = height * ((K_SCREEN_WIDTH - 15) / 2) + ((height + 1)* 5);
200     return collectionViewHeight;
201 }
202 @end

 

写的挺详细哟!!!有问题的可以找我要Demo哟!!

也可以加我的QQ: 1299625033....我们可以一起交流问题,一起学习!!!

 

posted on 2016-09-29 17:48  玉思盈蝶  阅读(374)  评论(0编辑  收藏  举报

导航