步骤:
1.创建一个继承自UITableViewCell的类。
2.将cell中要用到的视图都声明成属性
3.将这些视图都添加到cell的contentView上
4.将数据对象也声明成属性,并重写数据对象的setter方法,将该数据模型的属性赋值给cell中的视图上。
5.根据文本内容自定义cell的高度的主要代码:
.h文件中的代码:
@interface MyCell : UITableViewCell
//cell要用的视图
@property(nonatomic,strong) UILabel *nameLabel;
@property(nonatomic,strong) UILabel *introduceLabel;
//数据模型
@property(nonatomic,strong) Student *student;
//根据模型计算出整个cell的高度
+(CGFloat)calcHeightForCellWithStudent:(Student *)student;
@end
.m文件中的主要代码:
@implementation MyCell
//初始化方法
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[s