zoukankan      html  css  js  c++  java
  • UItableViewCell自适应高度的坑

    误区1:使用 : [self layoutSubviews];来提前更新控件frame,事实上这个并不能每次都能更新frame.

    正确的做法是:

    [self setNeedsLayout]; //设置标志

     [self layoutIfNeeded]; //执行更新

    误区2:ios8以后位于cell中label自使用高度并不一定要设置高度,这是因为ios可以根据cell的宽度以及你设置的约束来确定label宽度,从而自适应高度,但是我今天我发现我错了。事实上它是根据tableView的宽度来计算label宽度的。

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        
        OrderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_1"];
        if (cell == nil) {
            cell = [[OrderTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell_1"];
        }
        cell.orderType = self.type;
        cell.model = self.listData[indexPath.section];
        
        return cell.suggestionHeight;
    }

    如上面所述,这个cell最终并不会被添加至tableView中,我只是通过它来计算cell高度,然后我发现效果并不理想,有时候正确有时候错误这迷惑了我(注:计算出来的高度,总是和实际高度存在误差)。经历各种尝试后灵机一动换成5s模拟器,发现完全正确这时候我明白了系统是320宽度进行计算的,而实际的cell宽度确实375于是就可能会存在误差。明白这点后,我把cell中的label约束都改为width约束,结果如偿所预愿。

  • 相关阅读:
    Kubernetes 部署 Kafka & Zookeeper & Kafka Manager
    prometheus-operator监控traefik-Ingress组件状态
    k8s与dns--coredns的一些实战经验
    kubernetes Tekton-CI/CD 持续集成流水线
    jenkins pipeline语法
    (Go)16.Redis连接池的使用
    (Go)15.golang printf 格式化输出
    (Go)14. 如何读取YAML,JSON,INI等配置文件
    Dubbo引用Javassist外部框架
    Dubbo之Filter 原理
  • 原文地址:https://www.cnblogs.com/TengSys/p/6769088.html
Copyright © 2011-2022 走看看