iOS Autolayout 在tableView scrollView 适用 学习

 1  如何自动适应cell的高度

autolayout  里面 使用 systemLayoutSizeFittingSize 方法 (系统通过 已知的完整的Constraints和view的属性来计算高度)根据一个cell实例计算高度.  

优势:不需要写过多复杂的高度计算逻辑, 代码简洁.  强大

(1)首先依旧要在下面代理方法里实现读取cell 高度 heightForRowAtIndexPath:

(2)计算高度  还是要考虑两种情况

    第一 如果不知道高度,计算一遍,存储(尽量只计算一次,然后保存高度,计算需要布局 也是一个效率问题)

    第二 知道高度 直接读取

(3)关于cell的读取

第一种  纯代码 写法

这种 是我纯代码开发中经常用到的

   (1)cell 初始化要override 方法

 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  
 {  
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];  
        if (self)  {  

            // TODO:cell页面布局  
        }  
        return self;  
 }  

 (2)  dequeue 获取cell 为空 要创建新cell

    static  NSString *cellIdentifier = @"AHFHomeListCell";
    AHFHomeListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {//需要判空 ,如果为空 需要创建cell initWithStyle:reuseIdentifier

        cell = [[AHFHomeListCell alloc] initWithStyle:UITableViewCellStyleDefault

                                      reuseIdentifier:cellIdentifier];

        [cell.topicView setHandleSeletedOptionBlock:^(TopicOptions *options, TopicListModel *listModel,BOOL isOnlyRead) {

            NSString *optionId = isOnlyRead ? listModel.voted_option_id : options.option_id;

            [self openArticleVCArticleId:listModel.article_id img:listModel.banner andOptionId:[optionId integerValue] andIsRead:isOnlyRead];

        }];
    }

第二种 代码 加 注册cell

为tableView注册cell,使用registerClass:forCellReuseIdentifier:方法向数据源注册cell(注意是Class 即 [xxxxxCell class]

//register cell:
static
NSString *kCellIdentify = @"MyTableViewCell"; [self.tableView registerClass:[xxxxxCell class] forCellReuseIdentifier:kCellIdentify];
//
cellForRowAtIndexPath:
//第一种实现
//
dequeueReusableCellWithIdentifier:forIndexPath: 方法会直接调用注册(所以必须有注册方法),不需要判断cell空值了就
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyTableViewCell" forIndexPath:indexPath];
//第二种实现
//dequeueReusableCellWithIdentifier:可以注册 之后 不需要判空,可以不注册需要判空如果为空 就创建新cell
static MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentify];
if (!cell) {
//如果使用 registerClass: 注册过 则此处不需要处理 否则需要创建cell initWithStyle:reuseIdentifier
} 
//TODO:填充UI数据

第三种 使用xib 加 注册 cell

使用 tableView 的 registerNib:forCellReuseIdentifier:方法向数据源注册cell

//register cell:

[self.tableView registerNib:[UINib nibWithNibName:@"MyTableViewCell" bundle:nil] forCellReuseIdentifier:kCellIdentify];
//cellForRowAtIndexPath:
实现规则同第二种方法里面的相应代码讲解

关于两种注册: registerNib: 与 registerClass: 为什么可以不用去判空 ?

因为无可复用cell时runtime将使用注册时提供的资源去新建一个cell并返回

  2   如何在ScrollView中使用Autolayout (这里用Masonry 纯代码实    CGFloat scrollWidth = HorizontalFrom750(225 + 20);

    CGFloat scrollHeight =VerticalFrom750(290);
    pageWidth = scrollWidth;
//设置scrollView 约束 [self.scrollView mas_makeConstraints:
^(MASConstraintMaker *make) { make.center.equalTo(self); make.width.equalTo(@(scrollWidth)); make.height.equalTo(@(scrollHeight)); }];
//使用containView 作为容器View 在容器View里面塞入目标滚动的子对象 UIView
*containView = UIView.new; [self.scrollView addSubview:containView]; [containView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.scrollView); make.height.equalTo(self.scrollView); }];
self.cardViewArray
= [NSMutableArray array]; self.cardButtonArray = [NSMutableArray array]; // 目标滚动元素 一个一个 add 在 conttainView上 注意边界问题 UIView *lastView; CGFloat leftPadding = 0; //边界 for (int i = 0; i < pageCount; i ++) { UIView *backView = UIView.new; [containView addSubview:backView]; [backView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(containView); make.width.equalTo(@(scrollWidth)); make.height.equalTo(self.scrollView.mas_height); if (lastView) { make.left.equalTo(lastView.mas_right); } else { make.left.equalTo(@(leftPadding)); } }]; lastView = backView; MethodMediaModel *music = self.musicArray[i]; MusicCardView *cardView = [[MusicCardView alloc]initWithFrame:CGRectZero]; [cardView setCardTitle:music.title imgUrl:music.img_url]; [cardView.playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside]; cardView.playButton.tag = i; [cardView.playButton setObj:music]; [backView addSubview:cardView]; [cardView mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(backView); make.left.equalTo(backView).offset(10); make.right.equalTo(backView).offset(- 10); make.height.equalTo(backView); }]; [self.cardViewArray addObject:backView]; [self.cardButtonArray addObject:cardView.playButton]; } [lastView mas_updateConstraints:^(MASConstraintMaker *make) { make.right.equalTo(containView.mas_right);//边界
}];

  3   使用Autolayout做动画

     //TODO:

  4      Autolayout在IOS6上的坑

    //TODO:

参考:

https://blog.cnbluebox.com/blog/2015/02/02/autolayout2/

http://blog.csdn.net/youngsblog/article/details/44536143

原文地址:https://www.cnblogs.com/someonelikeyou/p/6118180.html