autolayout在cell高度变化中的应用

情景说明:一个cell中有label,有的label大小固定,有的lable大小随文字改变,在最下面的是一张图片,大小也不固定(只给左上约束属于填充类型的,在xib中要先给一张图片,否则会有约束错误)

//在这个里面传递数据,注意:如果pictureView不存在,则需再cell的set方法中将image置为nil(否则它会加载xib中的image)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 设置 cell 的内容
    YZJCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.weibo = self.weiboList[indexPath.row];
    
    // 通知 cell 更新布局->就可以知道 cell 的准确高度了,这个一定要写
    [cell layoutIfNeeded];

    // 自动布局之后,获得 label 的紧凑的尺寸,如果label在cell的最下面就写这个
//    CGSize labelSize = [cell.contentLabel systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
//    return cell.contentLabel.frame.origin.y + labelSize.height + 20 + cell.pictureHeightConstraint.constant;


    CGSize iconSize = [cell.pictureView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    return cell.pictureView.frame.origin.y + iconSize.height;
}

 
posted @ 2015-04-28 01:47  boy332  阅读(200)  评论(0编辑  收藏  举报