1.自定义cell
自定义cell就是创建一个UITableViewCell的子类,子视图控件添加到cell的contentView上。
2.cell与model通信
1)cell中声明一个model类型的属性
2)重写model的setter方法,把model的各个属性值赋值给cell上的控件
注意:cell重用的时候是根据重用标示符在重用队列中取出对应类型的cell
3.cell的自适应高度
根据文本的长短,动态确定cell的高度
(student中的自我介绍introduce)
自定义ShuangCell.m中:
#pragma mark 根据模型(内容)计算高度
-(CGFloat)calcHeightWithStudent:(Student *)student{
//1.设置计算所在的最大范围
CGSize maxSize = CGSizeMake(_introduceLable.frame.size.width, 1000);
//2.创建字典,包含字体大小
NSDictionary *dict = @{NSFontAttributeName:_introduceLable.font};
//3.使用方法,计算文字的frame(计算一段文本在限定宽高内所占矩形大小)
CGRect frame = [student.introduce boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
//4.返回frame的高度值
return frame.size.height;
}
#pragma mark 根据模型计算出整个cell的高度
+(CGFloat)calcHeightForCellWithStudent:(Student *)student{
//创建一个对象,执行计算lable高度的方法,获取高度
CGFloat lableHeight = [[[ShuangCell alloc] init] calcHeightWithStudent:student];
//返回可变的高度 + 固定的高度即可
return 70 + lableHeight;
}
然后再在tabelViewController中的设置cell的高度的方法中调用此方法