您的位置:首页 > 其它

tableview非等高的cell

2016-06-03 10:54 169 查看
// - 方法1

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    QGMyCell * cell = [QGMyCell cellWithTableView:tableView];
    cell.model = _dataArr[indexPath.row];

// - 强制 cell 重新布局 这样 在 cell 中计算的高度和 cell 的空间的位置才可以实时刷新,否则 cell 会直接调用 heightForRowAtIndexPath 方法 这时cell 中的布局还没有完成 model 中的高度属性是没有值的

    [cell layoutIfNeeded];

    return cell;

}

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

    NSLog(@"heightForRowAtIndexPath -----%ld", (long)indexPath.row);

    return 250;

}

// - 估计 cell 的高度, 在没写这个方法时候, tableView会直接调用 heightForRowAtIndexPath  然后在调用 cellForRowAtIndexPath 而且会一次把所有的高度都计算出来这样会出现卡对现象 为了防止这种情况 就调用一下 estimatedHeightForRowAtIndexPath 方法 这样 就会 先调用cellForRowAtIndexPath 在调用heightForRowAtIndexPath  而且是 调用一次 cellForRowAtIndexPath
然后调用一次  heightForRowAtIndexPath 

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 200;
}

 /** 因为如果上边的方法 那么 Controller 中的代码的冗余度太高了 没用的代码太对了 为了防止这种情况 就 有了方法的代码(就是给 modle 的属性多加了个 rowHeight 然后在 cell 中  计算完控件的坐标  给 modle 的 cellHeight 赋值之前 执行一步    [self layoutIfNeeded];)  */

// - 方法2

#import "QGMyCell.h"

// - cell 从 xib 中布局获取到的时候 因为 xib 的自动布局问题 设置 _label.preferredMaxLayoutWidth = 320; 就是设置_ label 的布局的最大宽度是 320

- (void)awakeFromNib {

    [super awakeFromNib];

    _label.preferredMaxLayo
4000
utWidth = 320;

    // Initialization code

}

-(void)setUp{

    _label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 250)];

    _label.backgroundColor = [UIColor greenColor];

    _label.lineBreakMode = NSLineBreakByCharWrapping;

    _label.numberOfLines = 0;

    [self.contentView addSubview:_label];

}

-(void)setModel:(QGMyModel *)model{

    _label.text = model.title;

    

    NSDictionary * attr = @{    NSFontAttributeName : [UIFont systemFontOfSize:12]};

    CGSize size = [model.title boundingRectWithSize:CGSizeMake(320, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil].size;

    CGRect rect = _label.frame;

    rect.size.height = size.height;

    [self layoutIfNeeded];

    

    model.rowHeight = CGRectGetMaxY(_label.frame) + 10;

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