TableviewCell自适应高度之加载不同数量的图片。

23 篇文章 0 订阅
15 篇文章 0 订阅

题外话:项目中有这样的需求,商品评论可以发布文字和图片(不同数量的图片)

创建 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];

运行效果如图

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值