美文网首页
cell的高度

cell的高度

作者: 白河三 | 来源:发表于2017-02-24 15:54 被阅读12次

1.在model的增加cell height属性可以cellheight 重写get方法里计算cell的高度

@implementation NEPAboutClassModel

-(CGFloat)cellHeight

{

if (_cellHeight == 0.0) {

NSString *descStr = [NSString stringWithFormat:@"备注:%@",self.course.memo];

CGRect rect = [descStr boundingRectWithSize:CGSizeMake(NEP_ScreenWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil];

//文字是一行的状态下 区头加cell == 54 + 211

//在这里计算出tableView的内容大小,主要是计算备注这一栏label的大小

_cellHeight = 265 + rect.size.height - 21;

}

return _cellHeight;

}

@end
tableview在没有给预估高度的时候

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath  或  _tableView.estimatedRowHeight = 211.0f;

会先走

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

NEPAboutClassModel *acModel = self.model.courseinfo[indexPath.section];

return acModel.cellHeight;

}

然后走

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

NEPCourceDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NEPCourceDetailCell"];

if (cell == nil) {

cell = [NEPCourceDetailCell szl_viewFromXib];

}

cell.delegate = self;

NEPAboutClassModel *acModel = self.model.courseinfo[indexPath.section];

cell.model = acModel;

cell.selectionStyle = UITableViewCellSelectionStyleNone;

return cell;

}

2.在model的增加cell height属性可以cellheight 在cell的model属性的set方法里计算

- (void)setModel:(NEPCourceDelModel *)model

{

_model = model;

self.tableViewHeader.dateLb.text = [NSString stringWithFormat:@"%@    %@",model.time,[NSString stringFromPeriod:model.interval]];

self.tableViewHeader.totalLb.text = [NSString stringWithFormat:@"%d个约课邀请",model.countCourse];

[self.tableView reloadData];

//文字是一行的状态下 区头加cell == 54 + 211

//在这里计算出tableView的内容大小,主要是计算备注这一栏label的大小

model.cellHeight = self.tableView.contentSize.height;

}
这时tableview 要加一个预估高度才能保证

先进

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath

在进

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

NEPCourceDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NEPCourceDetailCell"];

if (cell == nil) {

cell = [NEPCourceDetailCell szl_viewFromXib];

}

cell.delegate = self;

NEPAboutClassModel *acModel = self.model.courseinfo[indexPath.section];

cell.model = acModel;

cell.selectionStyle = UITableViewCellSelectionStyleNone;

return cell;

}

再进

- (void)setModel:(NEPCourceDelModel *)model

{

_model = model;

self.tableViewHeader.dateLb.text = [NSString stringWithFormat:@"%@    %@",model.time,[NSString stringFromPeriod:model.interval]];

self.tableViewHeader.totalLb.text = [NSString stringWithFormat:@"%d个约课邀请",model.countCourse];

[self.tableView reloadData];

//文字是一行的状态下 区头加cell == 54 + 211

//在这里计算出tableView的内容大小,主要是计算备注这一栏label的大小

model.cellHeight = self.tableView.contentSize.height;

}

在进

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

NEPAboutClassModel *acModel = self.model.courseinfo[indexPath.section];

return acModel.cellHeight;

}

相关文章

网友评论

      本文标题: cell的高度

      本文链接:https://www.haomeiwen.com/subject/ocruwttx.html