Osheep

时光不回头,当下最重要。

Cell的自适应高度

  • 首先需要创建一个model类,继承于NSObject;
  • 定义两个属性:

@property(nonatomic,retain)NSString *imageName;
@property(nonatomic,retain)NSString *imageDescription;

  • 然后我们需要在ViewController里边创建tableView,并且遵守它的协议,实现两个必须遵守的代理方法。
 UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.backgroundColor = [UIColor orangeColor];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Model *model = self.modelArray[indexPath.row];
    
    static NSString *modelIdentifier = @"model";
    ModelCell *cell = [tableView dequeueReusableCellWithIdentifier:modelIdentifier];
    if (nil == cell) {
        cell = [[ModelCell alloc ] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:modelIdentifier];
    }
    cell.model = model;
    
    return cell;
}

  • 然后我们需要有一个方法来修改cell的高度:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //取出当前的model
    Model *model = self.modelArray[indexPath.row];
    CGSize imageSize = [UIImage imageNamed:model.imageName].size;
    CGFloat scale = tableView.bounds.size.width / imageSize.width;
    CGFloat imageViewHeight = imageSize.height * scale;
    //计算尺寸
    CGRect rect = [model.imageDescription boundingRectWithSize:CGSizeMake(tableView.bounds.size.width, 1000.f) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:20.f]} context:nil];
    
    return imageViewHeight + rect.size.height;
    
}
  • 下边是我们自定义cell的代码:
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.modelImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.modelImageView.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:self.modelImageView];
        
        self.modelImageDescriptionLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.modelImageDescriptionLabel.backgroundColor = [UIColor greenColor];
        self.modelImageDescriptionLabel.font = [UIFont systemFontOfSize:20.f];
        //设置label的默认显示文字的行数,0为自动;
        self.modelImageDescriptionLabel.numberOfLines = 0;
        [self.contentView addSubview:self.modelImageDescriptionLabel];
        
    }
    return self;
    
}
//重写setter方法。接收model,
-(void)setModel:(Model *)model {
    if (_model != model) {
        _model = model;
        self.modelImageView.image = [UIImage imageNamed:model.imageName];
        self.modelImageDescriptionLabel.text = model.imageDescription;
    }
}

-(void)layoutSubviews {
    [super layoutSubviews];
    //首先拿到原始图片的尺寸;
    CGSize imageSize = self.modelImageView.image.size;
    //求比例(宽和宽的比等于高和高的比)
    CGFloat scale = self.contentView.bounds.size.width / imageSize.width;
    self.modelImageView.frame = CGRectMake(0, 0, self.contentView.bounds.size.width,imageSize.height * scale);
    
    //字符串的计算文本高度的方法,
    //参数1:文本计算需要给定的尺寸
    //参数2:计算选项:(第一个是以行为单位去计算,第二个是参考字体去计算)
    //参数3:字符串的属性(是字典类型的,来设置字体的大小)
    //参数4:绘制上下文,填nil就可以,
    CGRect rect = [self.model.imageDescription boundingRectWithSize:CGSizeMake(self.contentView.frame.size.width, 1000.f) options: NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:20.f]} context:nil];
    
    self.modelImageDescriptionLabel.frame = CGRectMake(0, self.modelImageView.frame.origin.y + self.modelImageView.frame.size.height, self.contentView.frame.size.width, rect.size.height);
 
}

点赞