SDAutoLayout的使用

SDAutoLayout 下载链接

SDAutoLayout 基础版视频教程:http://www.letv.com/ptv/vplay/24038772.html 

SDAutoLayout 进阶版视频教程:http://www.letv.com/ptv/vplay/24381390.html 

SDAutoLayout 原理简介视频教程:http://www.iqiyi.com/w_19rt0tec4p.html 

个人觉得自适应TableView的高度这块比较好用,增加约束这一块的话,如果不是必须要纯代码编写项目,还是在Xib、storyboard里增加约束效率较高,对原Demo有兴趣的可以点击链接下载,以下只截取主要部分代码:

YXTableViewController.m

/*
 自适应TableView,有三种方式:
 1.最早版,需要三步:
 1.1 设置cell高度自适应:
 cell布局设置好之后调用此方法就可以实现高度自适应(注意:如果用高度自适应则不要再以cell的底边为参照去布局其子view)
 [cell setupAutoHeightWithBottomView:_view4 bottomMargin:10];
 
 1.2 在返回行数的代理方法里,增加一个方法[tableView startAutoCellHeightWithCellClass:[YXTableViewCell class] contentViewWidth:[UIScreen mainScreen].bounds.size.width],如下:
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    [tableView startAutoCellHeightWithCellClass:[YXTableViewCell class] contentViewWidth:[UIScreen mainScreen].bounds.size.width];
    return _contenArray.count;
 }
 
 1.3 在返回行高度的代理方法里返回高度,如下:
 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = [tableView cellHeightForIndexPath:indexPath model:_contenArray[indexPath.row] keyPath:@"model"];
    return height;
 }
 
 
 2.上面方法的简约版,其实就是把1.2和1.3两个方法合为一个方法,需要两步:
 2.1 设置cell高度自适应:
 // cell布局设置好之后调用此方法就可以实现高度自适应(注意:如果用高度自适应则不要再以cell的底边为参照去布局其子view)
 [cell setupAutoHeightWithBottomView:_view4 bottomMargin:10];
 
 2.2 获取自动计算出的cell高度
 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
 id model = self.modelsArray[indexPath.row];
 // 获取cell高度
 return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[DemoVC9Cell class]  contentViewWidth:cellContentViewWith];
 }
 
 3.升级版(适应于cell条数少于100的tableview):tableview 高度自适应设置只需要2步:
 3.1 设置cell高度自适应:
 // cell布局设置好之后调用此方法就可以实现高度自适应(注意:如果用高度自适应则不要再以cell的底边为参照去布局其子view)
 [cell setupAutoHeightWithBottomView:_view4 bottomMargin:10];
 
 3.2 >> 获取自动计算出的cell高度
 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // 获取cell高度
 return [self cellHeightForIndexPath:indexPath cellContentViewWidth:[UIScreen mainScreen].bounds.size.width];
 }
*/

#import "YXTableViewController.h"
#import "YXTableViewModel.h"

@interface YXTableViewController ()
{
    NSMutableArray *_contenArray;
}

@end

@implementation YXTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (!_cellClassName) {
        _cellClassName = @"YXTableViewCell";
    }
    
    _contenArray = [YXTableViewModel getSourceData];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    Class cellClass = NSClassFromString(_cellClassName);
    CGFloat height = [tableView cellHeightForIndexPath:indexPath model:_contenArray[indexPath.row] keyPath:@"model" cellClass:[cellClass class] contentViewWidth:[UIScreen mainScreen].bounds.size.width];
    return height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:_cellClassName];
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:_cellClassName owner:nil options:nil][0];
    }
    [cell setValue:_contenArray[indexPath.row] forKey:@"model"];
    return cell;
}

@end

YXTableViewCell.m

@implementation YXTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    _iconImgVi = [UIImageView new];
    _iconImgVi.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:_iconImgVi];
    _iconImgVi.sd_layout.leftSpaceToView(self.contentView,8)//距离左边参照物8像素
    .topSpaceToView(self.contentView,8)//距离上边参照物8像素
    .widthIs(50)//宽等于50像素
    .heightIs(50);//高等于50像素
    
    _titleLab = [UILabel new];
    _titleLab.backgroundColor = [UIColor lightGrayColor];
    [self.contentView addSubview:_titleLab];
    _titleLab.sd_layout.leftSpaceToView(_iconImgVi,8)
    .topSpaceToView(self.contentView,8)
    .rightSpaceToView(self.contentView,8)
    .heightRatioToView(_iconImgVi,0.4);//高度等于_iconImgVi高的0.4倍数
    
    _contentLab = [UILabel new];
    _contentLab.backgroundColor = [UIColor greenColor];
    [self.contentView addSubview:_contentLab];
    _contentLab.sd_layout.leftEqualToView(_titleLab)//_contentLab的x==_titleLab的x
    .topSpaceToView(_titleLab,8)
    .rightEqualToView(_titleLab)//_contentLab距离右边参照物的空间==_titleLab距离右边参照物的空间
    .autoHeightRatio(0)//根据文字内容,自适应高度
    .minHeightIs(10);//最小高度为10像素
    
    _describeLab = [UILabel new];
    _describeLab.backgroundColor = [UIColor cyanColor];
    [self.contentView addSubview:_describeLab];
    _describeLab.sd_layout.leftEqualToView(_titleLab)
    .topSpaceToView(_contentLab,8)
    .rightEqualToView(_titleLab)
    .autoHeightRatio(0);
    
//    cell高度自适应
    [self setupAutoHeightWithBottomView:_describeLab bottomMargin:8];
}

- (void)setModel:(YXTableViewModel *)model {
    self.titleLab.text = model.titleStr;
    self.contentLab.text = model.contentStr;
    self.describeLab.text = model.describeStr;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
这里采用的是单Cell第二种方法,
2.1 设置cell高度自适应:
 // cell布局设置好之后调用此方法就可以实现高度自适应(注意:如果用高度自适应则不要再以cell的底边为参照去布局其子view)
 [cell setupAutoHeightWithBottomView:_view4 bottomMargin:10];
 
 2.2 获取自动计算出的cell高度
 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
 id model = self.modelsArray[indexPath.row];
 // 获取cell高度
 return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[DemoVC9Cell class]  contentViewWidth:cellContentViewWith];
 }
Demo里有多Cell的自适应方法,原理是一样的,还有定义自适应控件和配合用Xib增加约束Cell的自适应,非常简单,感兴趣的可以看一下。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值