UITableViewCell使用Masonry进行自动计算行高的问题
来源:互联网 发布:ubuntu安装netsnmp 编辑:程序博客网 时间:2024/04/30 23:50
UITableViewCell使用Masonry进行自动计算行高的问题
开发中,Masonry进行UI界面的约束,越来越方便,但是在含有label内容不定的cell上,约束很容易出问题,一旦设置不当,就会显示不正常![不正常的约束](http://img.blog.csdn.net/20161020091611108)百度各种解决方法,发现都不太满意,不是思路复杂,就是方法感觉不够简洁,摸索了一阵子,在朋友的帮助下,终于找到了自我感觉最佳的处理方式
Masonry进行cell自动计算行高第一步
//控制器- (void)viewDidLoad { [super viewDidLoad]; //设置自动计算行号模式 self.tableView.rowHeight = UITableViewAutomaticDimension; //设置预估行高 self.tableView.estimatedRowHeight = 200;}
Masonry进行cell自动计算行高第二步
//自定义cell- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { //图片 UIImageView *imgview = [[UIImageView alloc] init]; //一定要添加到self.contentView上 [self.contentView addSubview:imgview]; self.imgview = imgview; //标题 UILabel *titleLabel = [[UILabel alloc] init]; titleLabel.textColor = [UIColor colorWithRed:251.0/255 green:177.0/255 blue:84.0/255 alpha:1.0]; titleLabel.font = [UIFont systemFontOfSize:18.0]; [self.contentView addSubview:titleLabel]; self.titleLabel = titleLabel; //内容 UILabel *detailLabel = [[UILabel alloc] init]; //关键的第二步 //1.numberOfLines 设置为0,label的文本会自动换行 detailLabel.numberOfLines = 0; //2.设置换行的模式,CharWrapping是以字符作为分割,WordWrapping是以单词作为分割,英文label特别明显 detailLabel.lineBreakMode = NSLineBreakByCharWrapping; detailLabel.font = [UIFont systemFontOfSize:13.0]; detailLabel.textColor = [UIColor colorWithRed:72.0/255 green:68.0/255 blue:69.0/255 alpha:1.0]; [self.contentView addSubview:detailLabel]; self.detailLabel = detailLabel; } return self;}
Masonry进行cell自动计算行高第三步
//layoutSubviews布局里的重点- (void)layoutSubviews { [super layoutSubviews]; //所有子控件,都要依赖与self.contentView作为约束父控件,而不是self(cell) [self.imgview mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(self.contentView.mas_centerY); make.left.mas_equalTo(self.contentView.mas_left).offset(34); make.width.height.mas_equalTo(70); }]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.contentView.mas_top).offset(15); make.left.mas_equalTo(self.imgview.mas_right).offset(24); make.right.mas_equalTo(self.contentView.mas_right).offset(-20); }]; [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(13); make.left.mas_equalTo(self.imgview.mas_right).offset(24); make.right.mas_equalTo(self.contentView.mas_right).offset(-25); //MARK:自动计算行高第四步---根据大家反映,更新后的代码 make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-10); }]; //MARK:自动计算行高第四步------Xcode 7.3下这样处理没问题,但是升级到Xcode 8.0后就不行了// [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {// make.top.mas_equalTo(self.mas_top);// make.left.mas_equalTo(self.mas_left);// make.right.mas_equalTo(self.mas_right);// make.bottom.mas_equalTo(self.detailLabel.mas_bottom).offset(10);// }];}
最关键的一步 [cell layoutIfNeeded];
Masonry进行cell自动计算行高第四步
//控制器初始化cell的时候- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { JDGGoldPacketsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JDGGoldPacketsCell" forIndexPath:indexPath]; if (cell == nil) { cell = [[JDGGoldPacketsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"JDGGoldPacketsCell"]; } self.goldPacketsCell = cell; //最关键的一步,解决不正常显示问题 [cell layoutIfNeeded]; return cell; }else { JDGGoldDetailCell *cell = [JDGGoldDetailCell cellWithTableView:tableView]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.aboutRedPackets = self.aboutRedPacketsArr[indexPath.row]; //最关键的一步,解决不正常显示问题 [cell layoutIfNeeded]; return cell; }}
如此,就完美解决使用Masonry约束cell自动计算行高的问题,就完美解决了,并不需要网上提供的那么复杂的方法,需要计算各种东东,嘻嘻.
0 0
- UITableViewCell使用Masonry进行自动计算行高的问题
- Masonry自动计算行高
- Masonry自动计算行高
- Masonry自动计算cell行高:HYBMasonryAutoCellHeight
- Masonry自动计算cell行高:HYBMasonryAutoCellHeight
- 自动计算UITableViewCell的高度
- UITableViewCell 自动计算高度遇到的一些诡异问题
- 基于Masonry自动计算cell的高度
- UITableViewCell使用自动布局计算高度
- size classes的使用总结&自动计算行高
- UITableViewCell使用AutoLayout自动布局遇到的问题
- Masonry代码自动布局的简单使用。
- Masonry自动布局的使用心得
- Masonry自动布局使用
- 使用UITableViewCell遇到的问题
- 使用Autolayout时UITableViewCell的高度计算
- UITableViewCell使用自动布局的“最佳实践”
- 【iOS自动约束】使用Masonry导致内存持续增长问题分析
- Android自定义dialog从屏幕底部弹出并且充满屏幕宽度
- linux mysql 操作命令
- spring-boot 实例
- java中Executor、ExecutorService、ThreadPoolExecutor介绍
- Logy
- UITableViewCell使用Masonry进行自动计算行高的问题
- 新小区车位能不能买 看清这三大前提
- 在linux下用echo命令来清理日志,清理完毕之后不需要重启
- Android新特性之CardView的简单使用
- Apache出现Error:Apache shutdown unexpectedly怎么办?
- PostgreSQL学习笔记---2 启动PostgreSQL
- IE6下几个小BUG解决方法
- Edit Distance
- 各种排序算法性能调优