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

    https://lvwenhan.com/ios/449.html

    #import "ViewController.h"
    #import "MyTableViewCell.h"
    static NSString *cellIdentifier = @"mycell";
    
    @interface ViewController () <UITableViewDelegate, UITableViewDataSource>
    @property (strong, nonatomic) NSArray *listArr;
    @property (strong, nonatomic) MyTableViewCell *cell;
    @end
    
    @implementation ViewController
    @synthesize listArr;
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        [self.tableView registerNib:[UINib nibWithNibName:@"MyTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:cellIdentifier];
        self.tableView.estimatedRowHeight = 44;//很重要保障滑动流畅性
        self.cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        
        self.listArr = @[@"Do any additional setup after loading the view, typically from a nib.",
                         @"test",
                         @"UITableViewCell 高度自适应一直是我们做动态Cell高度时遇到的最烦躁的问题,Cell动态高度计算可以去看看 sunny 的这篇文章介绍,今天主要和大家分享下我在使用 systemLayoutSizeFittingSize 系统自带方法计算高度的一些心得!"];
    }
    
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return listArr.count;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
        MyTableViewCell *cell = self.cell;
        cell.myLabel.text = listArr[indexPath.row];
        cell.contentView.translatesAutoresizingMaskIntoConstraints = NO;
        CGFloat fittingHeight = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
        return fittingHeight;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        self.cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        
        if (self.cell == nil) {
            UINib *nib = [UINib nibWithNibName:@"MyTableViewCell" bundle:nil];
            [tableView registerNib:nib forCellReuseIdentifier:cellIdentifier];
            self.cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        }
        self.cell.myLabel.backgroundColor = [UIColor blueColor];
        self.cell.myLabel.textColor = [UIColor whiteColor];
        self.cell.myLabel.text = [listArr objectAtIndex:indexPath.row];
        return self.cell;
    }
    
    @end
  • 相关阅读:
    基于vue2.0 +vuex+ element-ui后台管理系统:包括本地开发调试详细步骤
    require.js实现js模块化编程(二):RequireJS Optimizer
    require.js实现js模块化编程(一)
    树型权限管理插件:jQuery Tree Multiselect详细使用指南
    表格组件神器:bootstrap table详细使用指南
    后台管理系统中的重点知识大全
    Ajax最详细的参数解析和场景应用
    npm常用命令小结
    详解javascript,ES5标准中新增的几种高效Object操作方法
    git入门学习(二):新建分支/上传代码/删除分支
  • 原文地址:https://www.cnblogs.com/lihaibo-Leao/p/5919570.html
Copyright © 2011-2022 走看看