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
时间: 2024-08-11 07:44:49

autolayout 高度自适应的相关文章

iOS UITableView+FDTemplateLayoutCell 配合AutoLayout分分钟教你实现动态高度自适应

UITableView里面的Cell固定高度那是基本不可能了,很多功能和界面都会涉及到高度自适应,而且电商类的尤其普遍,之前都是自己算啊算,代码写的非常多,逻辑还没写,光这布局UI和高度计算都能接近1000了,写完之后关键出点Bug整个人都不好了 当时的是这样的: 突然在github上看到UITableView+FDTemplateLayoutCell这个库 传送门:点击打开链接 刚看到的时候是这样的: 能  用   么 ??? 真  的  这  么 叼 ??? 第一次用的时候是这样的: 哥们,

uitableviewcell高度自适应笔记

今天看了几篇uitableviewcell高度自适应的文章,大体分为两种方式. 第一种方式,cell里面有label,在cellforrow绘制的时候计算Label的可能高度,并且在此时重新计算cell的高度,然后在heightforrow的时候返回这个cell新的高度.也就是说这些cell全部是计算出来的,根据里面的label的高度(label为contentView).http://blog.csdn.net/swingpyzf/article/details/18093959 第二种方式是

UITableViewCell高度自适应的关键点

iOS开发中对于UITableViewCell高度自适应的文章已经很多很多,但如果cell内容比较复杂,刚使用autolayout配置自使用时还是总不能一次性成功. KEY POINT 这里只说设置的关键一点: Cell内部的Constraints一定要有一条从Cell顶部到底部的一条可联通线. 图例列表: Paste_Image.png Paste_Image.png Paste_Image.png 最后顶部元素居上和底部元素距底部,加上约束即可.这条线上可以有固定高度的元素,可以有自适应高度

UI_11 自定义UITableViewCell、Cell的高度自适应

UITableViewCell很难满足我们的需求,因此,CustomCell(自定义单元格)至关重要.下面将通过一个例子演示自定义Cell.第二部分演示根据文本内容自适应Label.Cell高度. 第一部分 CustomCell的创建 1.创建DemoTableViewController,继承自UITableViewController,并设置其为window的根视图 AppDelegate.m - (BOOL)application:(UIApplication *)application

谷歌、火狐浏览器下实现JS跨域iframe高度自适应的完美解决方法,跨域调用JS不再是难题!

谷歌.火狐浏览器下实现JS跨域iframe高度自适应的解决方法 导读:今天开发的时候遇到个iframe自适应高度的问题,相信大家对这个不陌生,但是一般我们都是在同一个项目使用iframe嵌套页面,这个ifame高度自适应网上一搜一大把,今天要讲的如何在不同的网站下进行相互的调用跟在同一个网站下是一个效果:例如我在自己的项目里面Iframe  了第一博客的页面  http://www.diyibk.com/   当第一博客的页面高度变化了怎么通知父页面呢? 这个时候在谷歌下肯定是拿不到 ifram

一个定高,一个高度自适应的布局

Problem:父元素内包含两个子元素,一个定高,另一个高度自适应 Ans: <div class="box">    <div class="ele1"></div>    <div class="ele2"></div></div> (1)  .box {      width:200px;      height:300px;      background:red; 

textarea高度自适应问题

textarea中的文字如果过多,就会产生滚动条,一本分文本被遮盖住,不能看到所有的文本. 那么,如何才能让textarea的高度随输入内容多少,可以自动的改变高度呢? 解决思想: 1 利用contenteditable属性,让div变得可以编辑. 优点:方便,简介 缺点:提交数据不方便 2 利用js,动态改变textarea的高度 优点:提交数据很方便 缺点:要写好几段代码 我不禁提出质疑:在设计textarea这个标签的时候,专家们为何不给他一个可以高度自适应的属性呢? 谁能回答我?

textarea 高度自适应(chrome+ie8测试有效)

最近有一个需求,需要让textarea宽度不变,但是高度随着用户的输入高度自适应.由于浏览器版本的不兼容,很多方法通常只能在一个浏览器中起作用. 下面我写了一个小的demo,可以在chrome 和 ie8下正常使用,其他浏览器没有测试过. 第一种方式使用 js+css方式实现 <textarea rows='6' cols='100' style='overflow:scroll;overflow-y:visible;height:96px;width:679px;overflow-x:hidd

iframe高度自适应

第一次用iframe标签代替ajax异步刷新去做后天管理系统,发现iframe的确是个好东西.但有个最大的问题就是--高度不能自适应,要么设置死,要么用js去动态获取目标资源body的height并改变iframe的height.我们往往使用后者去自适应高度,这样的动态改变也有很多种方法--js和jQuery的.对于有选择强迫症的人来说,选一种通用的就行. HTML部分: 1 <div class="edit-content-iframe"> 2 <iframe sr