iOS 8 Tableview根据AutoLayout自动调整高度

原创Blog,转载请注明出处

blog.csdn.net/hello_hwc



前言:在iOS 8之前,如果要让Tableview根据内容自动调整大小的话,需要动态的去计算每个cell的高度。太尼玛操蛋了。iOS 8之后,可以根据AutoLayout来自动调整高度了,原理很简单。

  • DataSource中选择让iOS自动计算
  • 在Cell中,设定能够让iOS计算出高度的AutoLayout,注意,这里一定要是能够计算出高度的AutoLayout,这和传统的不一样。

效果


完整过程

新建一个基于singleview的工程,然后删除默认Storyboard的ViewController,拖拽一个TableviewController,设置为inital Controller



往Prototype Cells上拖拽两个UILabel

如图

设置cell 的reuse identifier为cell



为两个Label设置属性

Title

设置tag为10

Detail

设置tag为11



为两个Label设置AutoLayout

Title

Detail

注意,这里把title放在左上角,Detail放在左下角。然后添加二者之间的距离恒定为1,那么AutoLayout就会自动计算出高度。



新建一个TableviewController,并且讲storyboard上的tableviewController设置为新建的类



设置Tableview的高度为自动获取

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}

加入存储数据的数组,并且在初始化里设定数据

@property (strong,nonatomic)NSArray * titleArray;
@property (strong,nonatomic)NSArray * detailArray;
- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleArray = @[@"1",@"2",@"3"];
    self.detailArray = @[@"shot",@"Aduahguhauhguhaudghuahguhudhauhg",@"dhuahgudhaughuahdughuahguhauhguhdahudhuahughduahguhadguhaduhguadhughduahguahguhadugh"];
}

接下来就是Tablview的常用的,很好理解,这里不多赘述

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titleArray.count;
}
-(BOOL)prefersStatusBarHidden{
    return true;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    UILabel * titleLabel = (UILabel *)[cell viewWithTag:10];
    UILabel * contentLabel = (UILabel *)[cell viewWithTag:11];
    titleLabel.text = self.titleArray[indexPath.row];
    contentLabel.text = self.detailArray[indexPath.row];
    contentLabel.numberOfLines = 0;
    return cell;
}

然后,就得到了我们想要的效果了。


时间: 2024-08-06 08:58:07

iOS 8 Tableview根据AutoLayout自动调整高度的相关文章

iOS之UILabel根据内容自动调整高度

写法一:对象方法,传入:字体/最大尺寸. 即可得到宽高, 最大尺寸主要限制宽度,如果是一行就给个{MAXFLOAT,MAXFLOAT};如果是多行就限制X值,Y值随便给 - (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize { NSDictionary *attrs = @{NSFontAttributeName : font}; return [self boundingRectWithSize:maxSize option

iOS7上 使用autolayout让Cell自动调整高度

如果是iOS8, 那么在storyboard中对cell添加好约束之后只需要再添加两句代码就能让cell自动调整高度 1 self.tableView.estimatedRowHeight = 非0; 2 self.tableView.rowHeight = UITableViewAutomaticDimension; 但是现在大多数应用都还是需要支持iOS7的, 所以在以上基础上, 再在tableView的代理方法中添加以下即可解决, 在这之前别忘了添加一个属性 @property (stro

iOS 在TableView的Cell之间设置空白间隔空间

1.设置section的数目,即是你有多少个cell - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 3; // in your case, there are 3 cells } 2.对于每个section返回一个cell - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)secti

iOS: How To Make AutoLayout Work On A ScrollView

iOS: How To Make AutoLayout Work On A ScrollView Posted on June 11th, 2014 Ok, I’ll admit. I’ve been seriously struggling with AutoLayout ever since it’s been introduced. I understand the concept, and I LOVE the idea of it, but when I actually do it,

[iOS] Create TableView & customize UITableViewCell

1. First artical, notice the last thing - Connecting the DataSource and Delegate: http://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/ 2. Second Artical: https://medium.com/@musawiralishah/creating-custom-uitableviewcell-us

iOS开发tableView去掉顶部上部空表区域

tableview中的第一个cell 里上部 有空白区域,大概64像素 在viewDidLoad中加入如下代码 self.automaticallyAdjustsScrollViewInsets = NO; 原文地址:iOS开发tableView去掉顶部上部空表区域

iOS indexed tableview

http://stackoverflow.com/questions/18577462/how-to-make-a-tableview-divided-into-sections-by-letter-like-the-contacts-app http://www.iphonedevcentral.com/indexed-uitableview-tutorial/ http://furnacedigital.blogspot.com/2012/02/uitableview-sections.ht

IOS中TableView的用法

IOS中TableView的用法 一.UITableView 1.数据展示的条件 1> UITableView的所有数据都是由数据源(dataSource)提供的,所以要想在UITableView展示数据,必须设置UITableView的dataSource数据源对象 2> 要想当UITableView的dataSource对象,必须遵守UITableViewDataSource协议,实现相应的数据源方法 3> 当UITableView想要展示数据的时候,就会给数据源发送消息(调用数据源

iOS实现tableView下拉搜索功能

iOS实现tableView下拉搜索功能 地址:github地址 效果展示 JRSearchBar /// 搜索 -> array - (NSMutableArray *)searchTest:(NSString *)searchText InArray:(NSArray *)array;