iOS多种cell使用FDTemplateLayoutCell自动计算高度

本文介绍了在iOS开发中如何使用XIB文件和纯代码创建UITableViewCell,并利用Masonry布局实现自适应高度。通过实例展示了不同cell的注册方式及高度计算方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

两种cell,一种XIBcell,一种代码创建使用masonry布局的cell

项目git地址
效果图

注册cell

[_myTable registerNib:[UINib nibWithNibName:@"myIBCell" bundle:nil] forCellReuseIdentifier:@"ibcell"];
[_myTable registerClass:[masonryCell class] forCellReuseIdentifier:@"mcell"];

cell高度计算

  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
     if (indexPath.row<3) {
        return [tableView fd_heightForCellWithIdentifier:@"ibcell" configuration:^(id cell) {
            [self configCell:cell atindex:indexPath];
        }];

     }else{
        return [tableView fd_heightForCellWithIdentifier:@"mcell" configuration:^(id cell) {
            [self configCell:cell atindex:indexPath];
        }];
     }
    }

xib创建的cell就不多说了,下面贴上代码创建使用masonry布局的cell

//
//  masonryCell.m
//  autoheightCell
//
//  Created by jiang on 2017/4/21.
//  Copyright © 2017年 jiang. All rights reserved.
//

#import "masonryCell.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
@implementation masonryCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self =[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initUIandMas];
    }
    return self;
}
-(void)initUIandMas
{
    __weak typeof(self)wself = self;
    _contentImage=[[UIImageView alloc]init];
    [self.contentView addSubview:_contentImage];
    _name=[[UILabel alloc]init];
    [self.contentView addSubview:_name];
    [_contentImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.equalTo(wself.contentView).with.offset(20);
        make.right.equalTo(wself.contentView).with.offset(-20);
//        make.bottom.equalTo(_name.mas_top).with.offset(10);
        make.height.mas_equalTo(100);
    }];
    [_name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_contentImage);
        make.top.equalTo(_contentImage.mas_bottom).with.offset(10);
        make.height.mas_equalTo(20);
        make.bottom.equalTo(wself.contentView.mas_bottom).with.offset(-10);
    }];
}
-(void)setModel:(myModel *)model
{
    _name.text=model.name;
    [_contentImage sd_setImageWithURL:[NSURL URLWithString:model.imageUrl]];
    [_contentImage mas_updateConstraints:^(MASConstraintMaker *make) {
        float height= [model.height floatValue]/[model.width floatValue]*370;
        make.height.mas_equalTo(height);
        NSLog(@"height----%f",height);
    }];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值