美文网首页iOS基础控件
iOS 利用iOS8新特性(Self Sizing Cells)

iOS 利用iOS8新特性(Self Sizing Cells)

作者: XieHenry | 来源:发表于2017-03-09 14:36 被阅读1195次

在iOS 8中,苹果引入了UITableView的一项新功能--Self Sizing Cells,对于不少开发者来说这是新SDK中一项非常有用的新功能。在iOS 8之前,如果想在表视图中展示可变高度的动态内容时,你需要手动计算行高,而Self Sizing Cells为展示动态内容提供了一个解决方案。这个方法即对系统cell有效,也对xib创建的和自定义的cell有效。

1.以前我是这样计算高度的

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellStr = self.contentAry[indexPath.row];

   CGSize cellSize=[cellStr boundingRectWithSize:CGSizeMake(self.view.frame.size.width-20, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size;

    //cellSize的高度为文字的真实高度,需要上下都留出空白,这里+30的高度
    return cellSize.height+30;
}

如果是简单的文字,那么很简单的计算一下就OK了,但是如果附带图片,文字等内容呢?

现在研究了一下Self Sizing Cells,发现很好用。

2.系统方法计算高度

在这里我是配合Masonry进行坐标计算的。
在你创建tableview的地方添加,使用Self Sizing Cells时需要注意的事项:
1.为原型单元格定义Auto Layout约束
2.指定表视图的estimatedRowHeight
3.将表视图的rowHeight属性设置为UITableViewAutomaticDimension

self.tableView.estimatedRowHeight = 100; //  随便设个值
self.tableView.rowHeight = UITableViewAutomaticDimension;

下面贴上代码:

ViewController.h
//
//  ViewController.m
//  Tableview自适应高度
//
//  Created by XieHenry on 17/3/8.
//  Copyright © 2017年 XieHenry. All rights reserved.
//

#import "ViewController.h"
#import "TableViewCell.h"

@interface ViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSArray *contentAry;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"cell自适应高度";
    
    self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.estimatedRowHeight = 100;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    [self.view addSubview:self.tableView];

    
    
    self.contentAry = @[@"设置固定高度100,其余的设置为自适应高度--00",@"测试--01",@"测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试--02",@"测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试--03"];
    [self.tableView reloadData];
    
}

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

/*
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellStr = self.contentAry[indexPath.row];

   CGSize cellSize=[cellStr boundingRectWithSize:CGSizeMake(self.view.frame.size.width-20, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size;

    //cellSize的高度为文字的真实高度,需要上下都留出空白,这里+30的高度
    return cellSize.height+30;
}
*/

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //如果有必须的固定高度,可以设置固定的,没有固定的,可以设置自适应高度
    if (indexPath.row == 0) {
        return 100;
    } else {
        return UITableViewAutomaticDimension;
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.contentAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *ID = @"cell";
    //利用系统自带cell类型
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }

    cell.contentLabel.text = self.contentAry[indexPath.row];
    return cell;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

TableViewCell.h
#import <UIKit/UIKit.h>

@interface TableViewCell : UITableViewCell
@property (nonatomic,strong) UILabel *contentLabel;

@end
TableViewCell.m
#import "TableViewCell.h"
#import "Masonry.h"

@implementation TableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self createCell];
    }
    return self;
}
 
-(void)createCell{
    _contentLabel = [[UILabel alloc]init];
     _contentLabel.numberOfLines = 0;
    _contentLabel.font = [UIFont systemFontOfSize:13];
    [self.contentView addSubview:_contentLabel];

    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(10);
        make.right.mas_equalTo(self.contentView).offset(-10);
        make.top.mas_equalTo(self.contentView).offset(10);
        make.bottom.mas_equalTo(self.contentView).offset(-10);
    }];

    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor grayColor];
    [self.contentView addSubview:lineView];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentView);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-0);
        make.height.mas_equalTo(0.5);
    }];
}
@end

本文Demo

相关文章

网友评论

    本文标题:iOS 利用iOS8新特性(Self Sizing Cells)

    本文链接:https://www.haomeiwen.com/subject/hjewgttx.html