IOS tableView cell动态高度 (autoLayout)
来源:互联网 发布:虎门巨新网络老板 编辑:程序博客网 时间:2024/05/22 13:54
要做到使用autoLayout来生成动态高度的cell.cell里边子视图控件要使用autoLayout.
1.比如我定义的cell:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code [self configUserInterface]; } return self;}- (void)configUserInterface{ UIImageView *bgImageV = [[UIImageView alloc]init]; bgImageV.image = [[UIImage imageNamed:@"activityCellBox"]resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)]; [self addSubview:bgImageV]; [bgImageV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(@(12 * GY_Ratio)); make.right.equalTo(self.mas_right).offset(-12 * GY_Ratio); make.top.equalTo(@(15 * GY_Ratio)); make.bottom.equalTo(self.mas_bottom).offset(0); }]; _mainContentImageV = [[UIImageView alloc]init]; [bgImageV addSubview:_mainContentImageV]; [_mainContentImageV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(@0); make.right.equalTo(bgImageV.mas_right).offset(0); make.top.equalTo(bgImageV.mas_top); make.height.equalTo(@(70 * GY_Ratio)); }];
_activityContentLabel = [[UILabel alloc]init]; _activityContentLabel.numberOfLines = 0; [bgImageV addSubview:_activityContentLabel]; [_activityContentLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(@10); make.right.equalTo(bgImageV.mas_right).offset(0); make.top.equalTo(_mainContentImageV.mas_bottom).offset(5).priorityHigh(); make.bottom.equalTo(self.mas_bottom).offset(-1); }]; _activityContentLabel.font = [UIFont systemFontOfSize:14 * GY_Ratio]; _activityContentLabel.textColor = [UIColor colorFromHexString:@"#575656"]; _activityStatusImageV = [[UIImageView alloc]init]; [bgImageV addSubview:_activityStatusImageV]; _activityStatusImageV.image = [UIImage imageNamed:@"activityStartingBg"]; [_activityStatusImageV mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(bgImageV.mas_right).offset(-10 * GY_Ratio); make.bottom.equalTo(_mainContentImageV.mas_bottom).offset(0); make.size.equalTo(_activityStatusImageV); }]; _activityStatusLabel = [[UILabel alloc]init]; [_activityStatusImageV addSubview:_activityStatusLabel]; [_activityStatusLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(_activityStatusImageV.mas_left); make.centerY.equalTo(_activityStatusImageV.mas_centerY); make.right.equalTo(_activityStatusImageV.mas_right).offset(0); make.height.equalTo(@(20 * GY_Ratio)); }]; _activityStatusLabel.text = @"未知"; _activityStatusLabel.textColor = [UIColor whiteColor]; _activityStatusLabel.font = [UIFont systemFontOfSize:10 * GY_Ratio]; _activityStatusLabel.textAlignment = NSTextAlignmentCenter;}- (void)setContentWithModel:(GYActivityCenterModel *)model andPicSavePath:(NSString *)picSavePath{ NSString *picPathStr = [NSString stringWithFormat:@"%@/%@%@", API_ServerAddress, picSavePath, model.picName]; [_mainContentImageV sd_setImageWithURL:[NSURL URLWithString:picPathStr] placeholderImage:[UIImage imageNamed:@"isWinningBg"]]; _activityContentLabel.text = model.listDescription; float needHeight = [self getLabelHeigth:_activityContentLabel]; [_activityTitleLabel layoutIfNeeded]; [_activityContentLabel mas_updateConstraints:^(MASConstraintMaker *make) { make.height.equalTo(@(needHeight)); }];}#pragma mark - 获取label字符串所占的高度- (float)getLabelHeigth:(UILabel *)label{ CGSize size = [label.text boundingRectWithSize:(CGSize) {GY_MainWidth - 24 * GY_Ratio, 1000} options :NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes :@{NSFontAttributeName : label.font} context :nil].size; return size.height + 5;}
这里_activityContentLabel这个label可能所占用的高度不确定.所以在设置cell内容时,计算出其_activityContentLabel所占用的高度.其他控件的布局由于设置好的约束会随着改变布局.
2.我们要在tableViewDelegate 返回高度那儿
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ GYActivityCenterCell *cell = (GYActivityCenterCell *)[tableView dequeueReusableCellWithIdentifier:cellIntifier]; GYActivityCenterModel *model = dataSourceArray[indexPath.row]; [cell setContentWithModel:model andPicSavePath:activityCenterModelLsit.savePath]; ///这里是设置填充内容到cell中. [cell layoutIfNeeded]; CGFloat height = [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;///这里就得到cell的实际高度. return height;}
1 0
- IOS tableView cell动态高度 (autoLayout)
- iOS UITableView Cell动态高度(使用AutoLayout)
- ios TableView那些事(三十 五)TableView 单选操作使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- ios TableView那些事(三十 五)TableView 单选操作使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- iOS中使用AutoLayout动态调整Cell高度
- 【iOS知识学习】_iOS动态改变TableView Cell高度
- 【iOS知识学习】_iOS动态改变TableView Cell高度
- 【iOS知识学习】_iOS动态改变TableView Cell高度
- Swift iOS tableView static cell动态计算高度
- iOS tableview动态高度
- 使用autolayout自定义动态高度的cell
- UITableView使用AutoLayout动态计算cell高度
- IOS 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- iOS 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- 动态改变tableview的cell的高度
- MVVM动态计算Cell套TableView高度
- ios tableview 自动计算cell高度
- [iOS]TableView的Cell高度自适应
- shell 编程基础(3)
- 内存映射
- asp.net 邮件发送代码
- Check Style工具的使用
- spring junit完美解决方案
- IOS tableView cell动态高度 (autoLayout)
- if else的替代者
- golang 语法简介
- 基于Android Webview的Hybrid App开发的前端优化
- shell 编程基础(4)
- 涂鸦数据结构2 基本知识
- hive rcfile存储格式
- 转载-Oracle Flashback技术总结
- 体验dubbo的简单方法