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( + );

    CGFloat scrollHeight =VerticalFrom750();
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 = ; //边界
for (int i = ; 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();
make.right.equalTo(backView).offset(- );
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

iOS Autolayout 在tableView scrollView 适用 学习的更多相关文章

  1. iOS autoLayout总结

    本文转自 http://ruikq.github.io/ios/autolayout/uiscrollview/2015/01/27/iOS-autolayout%E6%80%BB%E7%BB%93. ...

  2. 从此爱上iOS Autolayout

    转:从此爱上iOS Autolayout 这篇不是autolayout教程,只是autolayout动员文章和经验之谈,在本文第五节友情链接和推荐中,我将附上足够大家熟练使用autolayout的教程 ...

  3. iOS 两个tableview的 瀑布流

    iOS 两个tableview的 瀑布流1. [代码]Objective-C     ////  DocViewController.m//  getrightbutton////  Created ...

  4. iOS安全些许经验和学习笔记

    http://bbs.pediy.com/showthread.php?t=209014 标题: [原创]iOS安全些许经验和学习笔记作者: MonkeyKey时间: 2016-03-30,16:32 ...

  5. 李洪强iOS开发之【零基础学习iOS开发】【01-前言】01-开篇

    从今天开始,我就开始更新[零基础学习iOS开发]这个专题.不管你是否涉足过IT领域,也不管你是理科生还是文科生,只要你对iOS开发感兴趣,都可以来阅读此专题.我尽量以通俗易懂的语言,让每个人都能够看懂 ...

  6. 解决TableView / ScrollView上的Menu问题(1滑出View区域还可点击2导致点击menu后View不能滑动)

    解决TableView / ScrollView上的Menu问题 1划出区域还可点击 重写CCMenu的触摸事件函数 TouchBegin/TouchMove/TouchCancle/TouchEnd ...

  7. 【转】IOS AutoLayout详解(三)用代码实现(附Demo下载)

    转载自:blog.csdn.net/hello_hwc IOS SDK详解 前言: 在开发的过程中,有时候创建View没办法通过Storyboard来进行,又需要AutoLayout,这时候用代码创建 ...

  8. IOS详解TableView——选项抽屉(天猫商品列表)

    在之前的有篇文章讲述了利用HeaderView来写类似QQ好友列表的表视图. 这里写的天猫抽屉其实也可以用该方法实现,具体到细节每个人也有所不同.这里采用的是点击cell对cell进行运动处理以展开“ ...

  9. iOS:在tableView中通过Masonry使用autolayout在iOS7系统出现约束崩溃

    一.出现崩溃情景: 给tableView创建一个头视图,也即tableHeaderView,然后使用Masonry并切换到iOS7/7.1系统给tableHeaderView中的所有子视图添加约束,此 ...

随机推荐

  1. mongodb更新器

    Name Description $inc Increments the value of the field by the specified amount. $mul Multiplies the ...

  2. MySQL 5.6修改data目录

    默认数据存放位置: C:\Documents and Settings\All Users\Application Data\MySQL\MySQL Server 5.6\ 打开该位置,即可看见my. ...

  3. Swift 开源 Linux Ubuntu Install

    Swift 开源了,它现在变成跨平台的了,开源后的 Swift 不止能运行在 MAC 和 iOS 平台,现在也可以运行在 Linux 平台了.swift.org 网站上面提供了在 Linux 上面安装 ...

  4. ORA-06519: 检测到活动的自治事务处理,已经回退

    写了一个函数,由于在定义时加入了 create or replace function F_计算结果(In_参数 varchar2) return number is --使用自治事务PRAGMA A ...

  5. struts.xml文件:

    struts.xml文件中包含的配置信息,你将修改所采取的措施的开发.这个文件可以被用来覆盖默认设置的应用程序,例如struts.devMode=false和其他设置中定义的属性文件.这个文件可以创建 ...

  6. Eclipse 浏览(Navigate)菜单

    浏览 Eclipse 工作空间 浏览(Navigate)菜单提供了多个菜单可以让你快速定位到指定资源. 上图中 Open Type, Open Type in Hierarchy 和 Open Res ...

  7. 用于ARM上的FFT与IFFT源代码-C语言

    /********************************************************************************* 程序名称:快速傅里叶变换(FFT) ...

  8. JavaScript入门:004—JS凝视的写法和基本运算符

    JS的凝视JS中加凝视和寻常写C#代码是几乎相同的.有//和/* */这两种.单行凝视使用双斜杠比如. <script type="text/javascript"> ...

  9. 这篇文章主要为大家详细介绍了jQuery密码强度验证控件使用详解的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    本文实例为大家分享了jQuery密码强度验证控件,供大家参考,具体内容如下 <html>   <head>     <meta http-equiv="Cont ...

  10. (转载)Unity中解析ini配置文件----INIParser

    大家好,我是孙广东.   转载请注明出处:http://blog.csdn.net/u010019717 更全的内容请看我的游戏蛮牛地址:http://www.unitymanual.com/spac ...