使用XIB的UITableViewCell自适应,以及出现的问题进行解决

1.首先需要定义一个属性

@property (nonatomic, strong) UITableViewCell *prototypeCell;

2.在创建完tableView后加上如下代码

 self.prototypeCell=[self.tableView dequeueReusableCellWithIdentifier:CVCellId];

3.在XIB中添加约束,此处不一一赘述


注意的一点时在XIB中对Label设置预估值,即



4.在heightForRowAtIndexPath中作如下修改

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    MineFixViewCell * cell=(MineFixViewCell *)self.prototypeCell;

    MineFixItem * item=[self.itemList objectAtIndex:indexPath.row];

    cell.item = item;

    CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

    MyLog(@"%f---%f",size.width,size.height);

    return 1  + size.height;

}

当完成后发现一个问题,即Label中文字显示不完全,如图


而后,在XIB中取消掉Preferred Width的勾选,而在

awakeFromNib中添加如下代码

 self.contentLabel.preferredMaxLayoutWidth = kUIScreenWidth - 80;

完美解决了,附图




原文地址:https://www.cnblogs.com/melons/p/5792037.html