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
posted on 2017-05-03 11:05 ACM_Someone like you 阅读(304) 评论(0) 编辑 收藏 举报
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
2016-05-03 iOS 单例模式 学习 "52个方法 第6章 45条 使用 dispath_once 来执行只需运行一次的线程安全代码"