zoukankan      html  css  js  c++  java
  • 动态计算UITableViewCell高度<进阶>

    @property (nonatomic, strong) UITableViewCell *prototypeCell;
    
    //注册Nib
    UINib *cellNib = [UINib nibWithNibName:@"NLGC" bundle:nil];
    [self.tableView registerNib:cellNib forCellReuseIdentifier:@"NLGC"];
    self.prototypeCell  = [self.tableView dequeueReusableCellWithIdentifier:@"NLGC"];
    
    //CellForIndexPath
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NLGC *cell = [self.tableView dequeueReusableCellWithIdentifier:@"NLGC"];
        cell.t.text = [self.tableData objectAtIndex:indexPath.row];
        return cell;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        NLGC *cell = (NLGC *)self.prototypeCell;
        cell.t.text = [self.tableData objectAtIndex:indexPath.row];
        CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
        NSLog(@"h=%f", size.height + 1);
        return 1  + size.height;
    }
    

      若文本控件是textView则要运用到sizeToFit

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
    NLGC *cell = (NLGC*)self.prototypeCell; 
    cell.t.text
    = [self.tableData objectAtIndex:indexPath.row];
    CGSize size
    = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    CGSize textViewSize
    = [cell.t sizeThatFits:CGSizeMake(cell.t.frame.size.width, FLT_MAX)];
    CGFloat h
    = size.height + textViewSize.height; h = h > 89 ? h : 89; //89是图片显示的最低高度, 见xib
    NSLog(@"h=%f", h);
    return 1 + h;
    }

    Manual Layout

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        C3 *cell = [self.tableView dequeueReusableCellWithIdentifier:@"C3"];
        cell.t.text = [self.tableData objectAtIndex:indexPath.row];
        [cell.t sizeToFit];
        return cell;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        C3 *cell = (C3 *)self.prototypeCell;
        NSString *str = [self.tableData objectAtIndex:indexPath.row];
        cell.t.text = str;
        CGSize s = [str calculateSize:CGSizeMake(cell.t.frame.size.width, FLT_MAX) font:cell.t.font];
        CGFloat defaultHeight = cell.contentView.frame.size.height;
        CGFloat height = s.height > defaultHeight ? s.height : defaultHeight;
        NSLog(@"h=%f", height);
        return 1  + height;
    }
    
    - (CGSize)calculateSize:(CGSize)size font:(UIFont *)font 
    {
        CGSize expectedLabelSize = CGSizeZero;
    
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
            NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
            paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
            NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy};
    
            expectedLabelSize = [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
        }
        else {
            expectedLabelSize = [self sizeWithFont:font
                                           constrainedToSize:size
                                               lineBreakMode:NSLineBreakByWordWrapping];
        }
    
        return CGSizeMake(ceil(expectedLabelSize.width), ceil(expectedLabelSize.height));
    }
    

      

    如有疑问,共同探讨,共同进步。
  • 相关阅读:
    keil 提示"running with code size limit 32k"
    关于C语言编译出现give arg types警告问题
    windows10添加设备管理器的快捷方式到桌面
    deepin20社区版 安装 STM32CubeIDE 小记
    STM32开发 printf和scanf函数的重定向——修改HAL标准库用printf函数发送数据直接输出
    ardupilot环境配置之eclipse指定jdk版本启动,解决“Version XXXX of the JVM is not ......"报错的问题
    jdk9,10,11,12没有jre安装方法
    C++ 类构造函数 & 析构函数
    STM32 Keil中关于stlink的调试 下载设置
    STM32 SWD下载出现no target connect解决方法
  • 原文地址:https://www.cnblogs.com/nielsen/p/6144262.html
Copyright © 2011-2022 走看看