题外话:项目中有这样的需求,商品评论可以发布文字和图片(不同数量的图片)
创建 UITableView
在 ViewController 里创建 UITableView
// 商品评论
_commentView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
_commentView.delegate = self;
_commentView.dataSource = self;
[_commentView registerClass:[KHUITVCell_Comment class] forCellReuseIdentifier:@"comment"];
_commentView.estimatedRowHeight = 45;
_commentView.rowHeight = UITableViewAutomaticDimension;
[self.view addSubview:_commentView];
自定义KHUITVCell_Comment
KHUITVCell_Comment.h
@interface KHUITVCell_Comment : UITVCell_base
@property(nonatomic,strong)CYBlock_ObjectIndexpath block_itemAtIndexpath;//点击图片查看图片
@end
KHUITVCell_Comment.m
注意这里用到的UICollectionViewLeftAlignFlowLayout参见文章:collectionView之LeftAlign的bug
@interface KHUITVCell_Comment()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UIButton *userAvatar;
@property(nonatomic,strong)UILabel *nameLabel;
@property(nonatomic,strong)UILabel *timeLabel;
@property(nonatomic,strong)SYStarRatingView *starView;
@property(nonatomic,strong)UILabel *commentLabel;//评论内容
@property(nonatomic,strong)UILabel *specificationLabel;//规格
@property(nonatomic,strong)UICollectionViewLeftAlignFlowLayout *flowout;
@property(nonatomic,strong)UICollectionView *imageCollectionView;
@end
@implementation KHUITVCell_Comment
-(void)addSubviews
{
[super addSubviews];
CGFloat hPadding = 5;
CGFloat vPadding = 5;
self.userAvatar = [UIButton buttonWithType:UIButtonTypeCustom];
self.userAvatar.layer.cornerRadius = 5;
self.userAvatar.layer.masksToBounds = YES;
[self.contentView addSubview:self.userAvatar];
self.nameLabel = [[UILabel alloc]init];
self.nameLabel.font = [UIFont systemFontOfSize:14];
self.nameLabel.textAlignment = NSTextAlignmentLeft;
// self.nameLabel.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.nameLabel.contentMode = UIViewContentModeCenter;
self.nameLabel.textColor = [UIColor darkGrayColor];
[self.contentView addSubview:self.nameLabel];
self.timeLabel = [[UILabel alloc]init];
self.timeLabel.font = [UIFont systemFontOfSize:13];
self.timeLabel.textColor = [UIColor lightGrayColor];
[self.contentView addSubview:self.timeLabel];
UIView *divideLine = [[UIView alloc]init];
divideLine.backgroundColor = [UIColor lightGrayColor];
[self.contentView addSubview:divideLine];
self.starView = [[SYStarRatingView alloc] initWithFrame:CGRectMake(5, 50, 100, 20) numberOfStar:5];
self.starView.foregroundImageName = @"serviceComment_startOn";
self.starView.backgroundImageName = @"serviceComment_startOff";
self.starView.delegate = nil;
self.starView.userInteractionEnabled = NO;
self.starView.showIntScore = YES;
self.starView.maxScore = 5;
[self.contentView addSubview:self.starView];
self.commentLabel = [[UILabel alloc]init];
self.commentLabel.font = [UIFont systemFontOfSize:14];
self.commentLabel.numberOfLines = 0;
self.commentLabel.textColor = [UIColor darkGrayColor];
[self.contentView addSubview:self.commentLabel];
self.specificationLabel = [[UILabel alloc]init];
self.specificationLabel.font = [UIFont systemFontOfSize:12];
self.specificationLabel.numberOfLines = 0;
self.specificationLabel.textColor = [UIColor lightGrayColor];
[self.contentView addSubview:self.specificationLabel];
self.flowout = [[UICollectionViewLeftAlignFlowLayout alloc]init];
CGFloat width = (kScreenWidth - 40)/3;
self.flowout.itemSize = CGSizeMake(width, width);
self.flowout.minimumLineSpacing = 5;
self.flowout.minimumInteritemSpacing = 10;
self.imageCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.flowout];
self.imageCollectionView.alwaysBounceVertical = NO;
self.imageCollectionView.bounces = NO;
self.imageCollectionView.showsVerticalScrollIndicator = NO;
self.imageCollectionView.backgroundColor = [UIColor whiteColor];
self.imageCollectionView.scrollEnabled = NO;
self.imageCollectionView.delegate = self;
self.imageCollectionView.dataSource = self;
[self.imageCollectionView registerClass:[KHIICVCell_ImageComment class] forCellWithReuseIdentifier:@"imageCell"];
[self.contentView addSubview:self.imageCollectionView];
UIView *superView = self.contentView;
[self.userAvatar mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(superView).offset(hPadding);
make.top.equalTo(superView).offset(vPadding);
make.height.equalTo(@(30));
make.width.equalTo(@(30));
}];
[self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.userAvatar.mas_right);
make.top.lessThanOrEqualTo(self.userAvatar);
make.bottom.lessThanOrEqualTo(self.userAvatar);
make.centerY.equalTo(self.userAvatar);
make.height.lessThanOrEqualTo(self.userAvatar);
}];
[self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.nameLabel.mas_right);
make.top.equalTo(self.nameLabel);
make.right.equalTo(superView).offset(-hPadding);
make.height.equalTo(self.nameLabel);
}];
[divideLine mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(superView);
make.top.equalTo(self.userAvatar.mas_bottom).offset(vPadding);
make.width.equalTo(superView).offset(-30);
make.height.equalTo(@(0.5));
}];
[self.starView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(divideLine.mas_bottom).offset(vPadding);
make.left.equalTo(superView).offset(hPadding);
make.right.lessThanOrEqualTo(superView);
make.width.equalTo(@(100));
make.height.equalTo(@(20));
}];
[self.commentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.starView.mas_bottom).offset(vPadding);
make.left.equalTo(superView).offset(hPadding);
make.right.equalTo(superView).offset(-hPadding);
// make.bottom.equalTo(superView).offset(-vPadding);
}];
[self.specificationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.commentLabel.mas_bottom).offset(vPadding);
make.left.equalTo(superView).offset(hPadding);
make.right.equalTo(superView).offset(-hPadding);
// make.bottom.equalTo(superView).offset(-vPadding);
}];
[self.imageCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView.mas_left).offset(hPadding);
make.top.equalTo(self.specificationLabel.mas_bottom);
make.right.equalTo(self.contentView.mas_right).offset(-hPadding);
make.bottom.equalTo(self.contentView);
}];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
KHModel_GoodsComment *mo = self.object;
return mo.attatchments.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CYUICVCell_base *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"imageCell" forIndexPath:indexPath];
KHModel_GoodsComment *mo = self.object;
cell.object = [mo.attatchments objectAtIndex:indexPath.row];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
if (self.block_itemAtIndexpath) {
self.block_itemAtIndexpath(self.object,indexPath);
}
}
- (void)setObject:(id)object
{
[super setObject:object];
KHModel_GoodsComment *mo = object;
[self.imageCollectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView.mas_left).offset(5);
make.top.equalTo(self.specificationLabel.mas_bottom);
make.right.equalTo(self.contentView.mas_right).offset(-5);
if (mo.attatchments.count <= 0) {
make.height.priorityHigh().equalTo(@(0));
} else {
make.height.priorityHigh().equalTo(@(ceil(mo.attatchments.count / 3.0) * (kScreenWidth - 40) / 3 + 5 * floorf(mo.attatchments.count) / 3));
}
make.bottom.equalTo(self.contentView);
}];
[self.imageCollectionView reloadData];
[self.userAvatar sd_setBackgroundImageWithURL:[NSURL URLWithString:mo.buyerImage] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"touxiang@3x"]];
self.nameLabel.text = mo.showName;
self.timeLabel.text = [SMGlobalMethod getTimeFromTimeSp:mo.evaluationTime];
[self.starView setScore:[mo.evaluation integerValue] withAnimation:NO];
self.commentLabel.text = mo.comment;//评论的文字
self.specificationLabel.text = mo.specification;
}
*主要代码
[self.imageCollectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView.mas_left).offset(5);
make.top.equalTo(self.specificationLabel.mas_bottom);
make.right.equalTo(self.contentView.mas_right).offset(-5);
if (mo.attatchments.count <= 0) {
make.height.priorityHigh().equalTo(@(0));
} else {
make.height.priorityHigh().equalTo(@(ceil(mo.attatchments.count / 3.0) * (kScreenWidth - 40) / 3 + 5 * floorf(mo.attatchments.count) / 3));
}
make.bottom.equalTo(self.contentView);
}];
[self.imageCollectionView reloadData];