autolayout 高度自适应
2024-10-09 22:10:21
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 = ;//很重要保障滑动流畅性
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 ;
} - (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
最新文章
- PHP 面向对象
- 自定义 placeholder 文本颜色
- 《深入理解Spark:核心思想与源码分析》——SparkContext的初始化(叔篇)——TaskScheduler的启动
- IE6支持max-height及min-height解决方法
- abstract class和interface 知多少!!!
- Json常见问题
- 利用DataTable快速批量导数据
- Why Does Qt Use Moc for Signals and Slots(QT官方的解释:GUI可以是动态的)
- map map
- 用SWF来代替传统的帧动画
- 初识RabbitMQ系列之一:简单介绍
- 【转】十分有用的linux shell学习总结
- ●POJ 3608 Bridge Across Islands
- JS原生 未来元素监听写法
- input标签checkbox选中触发事件的方法
- Openstack-Ceilometer-Alarm运行机制
- springboot 简单使用 activemq 接收消息
- 《Inside C#》笔记(十三) 多线程 上
- jquery mouseover与mouseenter区别
- 【LongIntervalRetries】让我们来实现支付宝的异步回调方案
热门文章
- dwz Esc关闭dialog 窗口
- 使用jetty工具包处理url参数成map
- 如何在IntelliJ IDEA中使用Git .ignore插件忽略不必要提交的文件
- 一步一步开发sniffer(Winpcap+MFC)(五)莫道无人能识君,其实我懂你的心——解析数据包(转)
- chrome 浏览器的插件权限有多大?
- GitHub下载安装以及开源项目
- producter-consumer 他山之石
- 《Redis入门指南(第2版)》读后感
- Chrome F12 温故而知新 :因为重定向导致清空Network信息
- 第三部分:Android 应用程序接口指南---第四节:动画和图形---第一章 属性动画及动画与图形概述