使用aulayout自适应uitableviewcell高度

在使用autolayout的项目中,单元格的自适应高度,可能有点复杂,特别是对于复杂一点的自定义单元格;那么下面就这个autolayout自适应单元格,经过代码来讲明:布局


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {字体

    SGSuggestionRootTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];网站

    cell.frame = CGRectMake(0, 0, CGRectGetWidth(tableView.frame), 0);spa

    //标记须要从新布局从新布局资源

    [cell.contentView setNeedsLayout];字符串

    //使布局当即生效it

    [cell.contentView layoutIfNeeded];io

//    id obj = [[NSArray alloc] objectAtIndex:indexPath.row]table

    CGRect questionRect = [@"大家网站有人乱发垃圾信息" boundingRectWithSize:CGSizeMake(cell.questionLabel.frame.size.widthobject

                                                                                          , CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14.]} context:nil];

    CGRect sugContentRect = [@"大家网站有人乱发垃圾信息,好多广告,但愿赶忙清除!" boundingRectWithSize:CGSizeMake(cell.sugContentLabel.frame.size.width

                                                                                        , CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14.]} context:nil];

    CGRect fbContentRect = [@"大家网站有人乱发垃圾信息,好多广告,但愿赶忙清除!发顺丰撒啊事发地点萨芬撒发顺丰的法师法师嘎斯发噶沙发沙发上防辐射的方式和发上防辐射的方式和发上防辐射的方式和" boundingRectWithSize:CGSizeMake(cell.fbContentLabel.frame.size.width

                                                                                        , CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14.]} context:nil];

    //15是固定的控件间的间隙

    CGFloat sum = questionRect.size.height + sugContentRect.size.height + fbContentRect.size.height + 15;

    return sum;

}


以上代码是获取计算单元格的高度,红色字体部分是须要注意的,由于是经过重用机制获取的cell的frame宽度有问题,因此从新设置合适的宽度,而后经过     [cell.contentView setNeedsLayout]; 标记为待从新布局,  经过    [cell.contentView layoutIfNeeded]; 当即调用 layoutsubview方法,从而获取questionLabel、sugContentLabel、fbContentLabel这三个控件根据约束条件生成的width,(为节省资源,可将红色代码放到viewdidload中) 而后经过boundingRectWithSize这个方法和width计算出显示字符串的bound, 获得这些bound后,根据实际状况进行运算获取,将要显示单元格的高度


 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 中设置label的高度,在下面的代码中使用的是masonry这个对手写autolayout进行封装的开源类库,代码如:

    [label mas_updateConstraints:^(MASConstraintMaker *make) {

        make.height.greaterThanOrEqualTo(@(rect.size.height));

    }];

须要注意的是,因为使用了autolayout,多行label高度须要设置为大于等于,而不能设置为等于

相关文章
相关标签/搜索