zoukankan      html  css  js  c++  java
  • UITableViewCell自适应高度

    方式一:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        //cell宽度取系统cell width;
        CGFloat contentwidth = self.tableView.frame.size.width;
        //字体大小和下面的 heightForCell保持一致;
        UIFont *font = [UIFont systemFontOfSize:17.0];
        //当前cell数据;
        NSString *contentText = [self.dataArray objectAtIndex:indexPath.row];
        //计算cell数据文本大小;
        CGSize contentSize = [contentText sizeWithFont:font constrainedToSize:CGSizeMake(contentwidth, 1000) lineBreakMode:NSLineBreakByCharWrapping];
       
    
        
        static NSString *CellIdentifier = @"Cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        
        //重置cell rect大小;
        CGRect cellRect = [cell.textLabel textRectForBounds:cell.textLabel.frame limitedToNumberOfLines:0];
        cellRect.size = contentSize;
        cell.textLabel.frame = cellRect;
        cell.textLabel.text = contentText;
        //设置cell的textLable可以多行文本显示;
        cell.textLabel.numberOfLines = 0;
        //设置字体大小和颜色;
        cell.textLabel.textColor = [UIColor blueColor];
        cell.textLabel.font = font;
    
        return cell;
    }
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //重新计算每行的cell的高度;
        CGFloat contentWith = self.tableView.frame.size.width;
        UIFont *font = [UIFont systemFontOfSize:17.0];
        NSString *content = [self.dataArray objectAtIndex:indexPath.row];
        CGSize contentSize = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWith, 1000) lineBreakMode:NSLineBreakByCharWrapping];
        
        return contentSize.height+25;
    }
    View Code

    方式二:

    UITableViewCell 里面的 layoutSubViews方法里面写

    //再画布重绘的时候,重置cell自适应高度;
    -(void)layoutSubviews
    {
        [super layoutSubviews];
        _cou_title.text = _dataModel.cou_title;
        _cou_publish_date.text = [NSString stringWithFormat:@"发布日期:%@",_dataModel.cou_publish_date];
        _cou_expiration_date.text = [NSString stringWithFormat:@"结束日期:%@",_dataModel.cou_expiration_date];
        _cou_description.text = _dataModel.cou_description;
    
    
        CGSize descriSize = [_dataModel.cou_description sizeWithFont:[UIFont systemFontOfSize:15.0] constrainedToSize:CGSizeMake(300, 200) lineBreakMode:NSLineBreakByCharWrapping];
        _cou_description.frame = CGRectMake(_cou_description.frame.origin.x, _cou_description.frame.origin.y,_cou_description.frame.size.width, descriSize.height);
        
    }
    View Code
  • 相关阅读:
    [2021.8集训Day10/JZOJ.3410]【GDOI2014模拟】Tree
    [2021.8集训Day10/JZOJ.3441]【NOIP2013模拟】小喵喵的新家
    [模板]模拟退火 / 洛谷 P1337 [JSOI2004]平衡点
    P1600 [NOIP2016 提高组] 天天爱跑步
    P4556 [Vani有约会]雨天的尾巴 /【模板】线段树合并
    selenium的三种等待
    python中socket、socketio、flask-socketio、WebSocket的区别与联系
    (十二)python3 迭代器
    (十一)python3 encode()和decode()
    (十)python3 生成器
  • 原文地址:https://www.cnblogs.com/cocoajin/p/3089131.html
Copyright © 2011-2022 走看看