您的位置:首页 > 其它

xib自定义cell之自适应高度

2016-08-17 09:27 281 查看
第一种:计算高度

label多行显示,计算size:

CGSize contentSize = [FXAFactory
fc_getStringSizeWith:str boundingRectWithSize:CGSizeMake(kSCREEN_WIDTH -
30, MAXFLOAT)
font:[UIFont
systemFontOfSize:14]];

第二种:用系统自带的方法
1、在自定义cell的.m文件中一定要加上label的宽度

如:self.detail.preferredMaxLayoutWidth = [UIScreen
mainScreen].bounds.size.width -
20;
2、在tableView的dataSouce中

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

{

    self.table.rowHeight =
UITableViewAutomaticDimension;//设置cell的高度为自动计算,只有才xib或者storyboard上自定义的cell才会生效,需要在xib中设置好约束  

    self.table.estimatedRowHeight =
240;//必须设置好预估值 

    return self.table.rowHeight;

}

第三种:获取复用的Cell后模拟赋值,然后取得Cell高度

1、在自定义cell的.m文件中一定要加上label的宽度

如:self.detail.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;

2、

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

{

    MainCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"MainCell"];

    cell.imgView.image = [UIImage
imageNamed:[self.dataSoruce[indexPath.row]
objectForKey:@"image"]];

    cell.text.text = [self.dataSoruce[indexPath.row]
objectForKey:@"name"];

    cell.detail.text = [self.dataSoruce[indexPath.row]
objectForKey:@"detail"];

    [cell.contentView
layoutIfNeeded];

    [cell.contentView
updateConstraintsIfNeeded];//更新约束

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

    return size.height +
1;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: