效果圖:
717EA841-8896-4B94-8D81-6654087CBA37.png
MVC
在controller中
1替饿、創(chuàng)建對象
@property(nonatomic, strong) UITableView *tableView; /**< tableview */
@property(nonatomic, strong) NSArray *array; /**< array */
@property(nonatomic, strong) NSMutableArray *cellModelArr; /**< 放model的數(shù)組 */
2、數(shù)組賦值
3贸典、將數(shù)據(jù)源數(shù)組中的數(shù)據(jù)放入model中盛垦,將model放入cellModelArr中
在model中
.h
1、@property(nonatomic, copy) NSString *name; /**< name */
@property(nonatomic, copy) NSString *detail; /**< detail */
@property(nonatomic, assign) CGFloat detailLabelHeight; /**< 行高 */
+ (id)buildModelWithDic:(NSDictionary *)dic;
.m
1瓤漏、
#import "UIView+LabelHeight.h"
注:這個分類是我寫的計算label高度的一個方法腾夯。
2、KVC賦值
+ (id)buildModelWithDic:(NSDictionary *)dic {
return [[Model alloc] initWithDic:dic];
}
- (id)initWithDic:(NSDictionary *)dic {
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dic];
}
return self;
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
}
3蔬充、重寫detail屬性的set方法
- (void)setDetail:(NSString *)detail {
_detail = nil;
_detail = detail;
CGFloat width = [UIScreen mainScreen].bounds.size.width - 16;
_cellHeight = [UIView getLabelHeightByWidth:width Title:_detail font:[UIFont systemFontOfSize:15]];
}
在這里set的重寫方法中蝶俱,通過拿到detailLabel中的數(shù)據(jù)(model中的detail)先行計算detailLabel的高度。
4饥漫、(計算label高度的category 方法)
+ (CGFloat)getLabelHeightByWidth:(CGFloat)width Title:(NSString *)title font:(UIFont *)font {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
label.text = title;
label.font = font;
label.numberOfLines = 0;
[label sizeToFit];
CGFloat height = label.frame.size.height;
return height;
}
在View中
1榨呆、自定義創(chuàng)建xibCell
170595EC-404D-46CC-B44A-2D1812EAB7E2.png
上面nameLabel 上下左右加約束,高度固定21庸队。 下面detailLabel上左右加約束积蜻,高度固定,將高度拖到對應(yīng)的.m中
2彻消、
.h中
聲明model的屬性
@property(nonatomic, strong) Model *cellModel; /**< cellModel */
3竿拆、
.m中
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *detailHeight;
4、重寫model的set方法
將model中計算得到的detailLabel的高度賦值給從xib脫出來的detailHeight
- (void)setCellModel:(Model *)cellModel {
_cellModel = cellModel;
self.nameLabel.text = _cellModel.name;
self.detailLabel.text = _cellModel.detail;
self.detailHeight.constant = _cellModel.cellHeight;
}
最后在controller中 tableview的代理方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyTableViewCell"];
if (!cell) {
NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"MyTableViewCell" owner:nil options:nil];
cell = [nibs lastObject];
}
cell.cellModel = [_cellModelArr objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
Model *model = [_cellModelArr objectAtIndex:indexPath.row];
return model.cellHeight + 30;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100.f;
}
這樣能基本滿足簡單的xibCell中l(wèi)abel的高度自適應(yīng)了