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
原创粉丝点击