iOS开发——使用Autolayout生成动态高度的TableViewCell单元格
步骤一、TableViewCell中使用Autolayout
要点:Cell的高度必须在Constraints中指明,但不能定死,需要让内部由内容决定高度的View决定动态高度。
如UILabel设置numberOfLines为0,设置好左右约束和上下相对位置的约束后就可以让Label的内在高度尺寸约束决定Label的高,即可让系统推断出整个cell的高。
步骤二、在Controller中设置TableView的属性
要点:
self.tableView.estimatedRowHeight = 54;//54为你估算的每个单元格的平均行高,方便系统计算滚动条大小等动作
self.tableView.rowHeight = UITableViewAutomaticDimension;//实际值为-1,让系统自动计算Cell的行高。
样例代码:(使用Masonry第三方Autolayout库)
Cell:
1: //
2: // NewsCell.h
3: // M04P20-新闻
4: //
5: // Created by 张泽阳 on 4/26/15.
6: // Copyright (c) 2015 张泽阳. All rights reserved.
7: //
8:
9: #import <UIKit/UIKit.h>
10: @class News;
11: @interface NewsCell : UITableViewCell
12:
13: @property (nonatomic,strong) UILabel* newsTitleLabel;
14: @property (nonatomic,strong) UILabel* newsAuthorLabel;
15: @property (nonatomic,strong) UILabel* newsCommentsLabel;
16: @property (nonatomic,strong) UIImageView* pictureImageView;
17: -(void)setContentData:(News*)news;
18: +(instancetype)newsCellWithTableView:(UITableView*)tableView;
19: @end
20:
21:
22: //
23: // NewsCell.m
24: // M04P20-新闻
25: //
26: // Created by 张泽阳 on 4/26/15.
27: // Copyright (c) 2015 张泽阳. All rights reserved.
28: //
29:
30: #import "NewsCell.h"
31: #import "News.h"
32: #import "Masonry.h"
33: @implementation NewsCell
34:
35: /**
36: * 懒加载
37: */
38: -(UILabel *)newsCommentsLabel{
39: if (!_newsCommentsLabel) {
40: _newsCommentsLabel = [[UILabel alloc]init];
41: }
42: return _newsCommentsLabel;
43: }
44: -(UILabel *)newsTitleLabel{
45: if (!_newsTitleLabel) {
46: _newsTitleLabel = [[UILabel alloc]init];
47:
48: }
49: return _newsTitleLabel;
50: }
51: -(UILabel *)newsAuthorLabel{
52: if (!_newsAuthorLabel) {
53: _newsAuthorLabel = [[UILabel alloc]init];
54: [self.contentView addSubview:_newsAuthorLabel];
55: }
56: return _newsAuthorLabel;
57: }
58: -(UIImageView *)pictureImageView{
59: if (!_pictureImageView) {
60: _pictureImageView = [[UIImageView alloc]init];
61: }
62: return _pictureImageView;
63: }
64: /**
65: * 便利构造器
66: */
67: +(instancetype)newsCellWithTableView:(UITableView *)tableView{
68: static NSString* ID = @"news";
69: NewsCell* cell = [tableView dequeueReusableCellWithIdentifier:ID];
70: if (!cell) {
71: cell = [[NewsCell alloc]initWIthTableView:tableView andID:ID];
72: }
73:
74: [cell setNeedsLayout];
75: [cell layoutIfNeeded];
76: return cell;
77: }
78: /**
79: * init中设置相对固定的数据
80: *
81: */
82: -(instancetype)initWIthTableView:(UITableView*)tablview andID:(NSString*)ID{
83: // static NSString* ID = @"news";
84: self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];//别忘了super init
85: [self.contentView addSubview:self.newsTitleLabel];
86: [self.contentView addSubview:self.newsAuthorLabel];
87: [self.contentView addSubview:self.pictureImageView];
88: [self.contentView addSubview:self.newsCommentsLabel];
89:
90: self.newsCommentsLabel.textColor = [UIColor grayColor];
91: self.newsCommentsLabel.font = [UIFont systemFontOfSize:12];
92: [self.newsCommentsLabel setHighlightedTextColor:[UIColor whiteColor]];
93: self.newsAuthorLabel.textColor = [UIColor grayColor];
94: self.newsAuthorLabel.font = [UIFont systemFontOfSize:12];
95: [self.newsAuthorLabel setHighlightedTextColor:[UIColor whiteColor]];
96:
97: self.newsTitleLabel.font = [UIFont boldSystemFontOfSize:15];
98: self.newsTitleLabel.numberOfLines = 0;
99:
100: [self.newsTitleLabel setHighlightedTextColor:[UIColor whiteColor]];
101: self.pictureImageView.contentMode = UIViewContentModeScaleAspectFit;
102:
103: return self;
104: }
105: /**
106: * 设置相对动态的数据
107: */
108: -(void)setContentData:(News *)news{
109: self.newsTitleLabel.text = news.title;
110: self.newsAuthorLabel.text = news.author;
111: self.newsCommentsLabel.text = [NSString stringWithFormat:@"评论:%d",news.comments];
112: self.pictureImageView.image = [UIImage imageNamed:news.icon];
113: }
114: /**
115: * 设置布局 记得调用super
116: */
117: -(void)layoutSubviews{
118: [super layoutSubviews];
119: [self.newsTitleLabel mas_makeConstraints:^(MASConstraintMaker *make){
120: make.top.equalTo(self.contentView.mas_top).mas_offset(8); ///offset???
121: make.left.equalTo(self.contentView.mas_left).mas_offset(8);
122: make.right.equalTo(self.pictureImageView.mas_left).mas_offset(-8);
123: }];
124:
125: [self.newsAuthorLabel mas_makeConstraints:^(MASConstraintMaker *make){
126: make.left.equalTo(self.newsTitleLabel);
127: make.top.greaterThanOrEqualTo(self.newsTitleLabel.mas_bottom).mas_offset(8);
128: make.bottom.equalTo(self.contentView.mas_bottom).mas_offset(-8);
129: }];
130:
131: [self.newsCommentsLabel mas_makeConstraints:^(MASConstraintMaker *make){
132: make.top.equalTo(self.newsAuthorLabel.mas_top);
133: make.right.equalTo(self.pictureImageView.mas_left).mas_offset(-8);
134: }];
135:
136: [self.pictureImageView mas_makeConstraints:^(MASConstraintMaker *make){
137: make.top.equalTo(self.contentView.mas_top).mas_offset(8);
138: make.right.equalTo(self.contentView.mas_right).mas_offset(-8);
139: make.bottom.equalTo(self.contentView.mas_bottom).mas_offset(-8);
140: make.width.mas_equalTo(120);
141: }];
142: }
143: /**
144: * 单元格被选中/取消选中时的表现
145: */
146: -(void)setSelected:(BOOL)selected animated:(BOOL)animated{
147: [super setSelected:selected animated:animated];
148: // self.newsTitleLabel.textColor = selected?([UIColor whiteColor]):([UIColor blackColor]);
149: // self.newsAuthorLabel.textColor = selected?([UIColor whiteColor]):([UIColor blackColor]);
150: // self.newsCommentsLabel.textColor = selected?([UIColor whiteColor]):([UIColor blackColor]);
151: }
152: @end
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Controller中:
1: //
2: // ViewController.h
3: // M04P20-新闻
4: //
5: // Created by 张泽阳 on 4/26/15.
6: // Copyright (c) 2015 张泽阳. All rights reserved.
7: //
8:
9: #import <UIKit/UIKit.h>
10:
11: @interface ViewController : UIViewController
12: @property (nonatomic,strong) NSArray* newses;
13:
14: @end
15:
16:
17: //
18: // ViewController.m
19: // M04P20-新闻
20: //
21: // Created by 张泽阳 on 4/26/15.
22: // Copyright (c) 2015 张泽阳. All rights reserved.
23: //
24:
25: #import "ViewController.h"
26: #import "News.h"
27: #import "NewsCell.h"
28: #import "NSObject+NSObject_ZZYMODEL.h"
29: @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
30: @property (weak, nonatomic) IBOutlet UITableView *tableView;
31:
32: @end
33:
34: @implementation ViewController
35: -(NSArray *)newses{
36: if (!_newses) {
37: _newses = [News modelArrayWithFilename:@"news.plist"];
38: }
39: return _newses;
40: }
41: - (void)viewDidLoad {
42: [super viewDidLoad];
43: self.tableView.estimatedRowHeight = 54;
44: self.tableView.rowHeight = UITableViewAutomaticDimension;
45: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidRotate) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
46: }
47: -(BOOL)prefersStatusBarHidden{
48: return YES;
49: }
50: -(void)orientationDidRotate{
51: [self.tableView reloadData];
52: }
53: -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
54: return 1;
55: }
56:
57: -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
58: return self.newses.count;
59: }
60:
61: -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
62: NewsCell* cell = [NewsCell newsCellWithTableView:tableView];
63: [cell setContentData:self.newses[indexPath.row]];
64:
65:
66: return cell;
67:
68: }
69:
70:
71:
72:
73: @end
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
参考连接:
http://www.mgenware.com/blog/?p=507
http://blog.163.com/china_uv/blog/static/1171372672014111681232340/
iOS开发——使用Autolayout生成动态高度的TableViewCell单元格的更多相关文章
- ios开发不能不知的动态修复bug补丁第三方库JSPatch 使用学习:JSPatch导入、和使用、.js文件传输加解密
JSPatch ios开发不能不知的动态修复bug补丁第三方库JSPatch 使用学习:JSPatch导入.和使用..js文件传输加解密 ios开发面临审核周期长,修复bug延迟等让人无奈的问题,所以 ...
- js如何实现动态点击改变单元格颜色?
js如何实现动态点击改变单元格颜色? 一.总结 1.通过table的rows属性,遍历表格所有行,然后通过cells属性,遍历每一行中的单元格. 2.遍历的过程中,动态的为每一个单元格定义单击事件,改 ...
- 使用Autolayout xib实现动态高度的TableViewCell
http://my.oschina.net/u/2360693/blog/481236?p={{totalPage}} 创建Xib文件 首先将Cell做好布局,调整到满意的位置和宽度,然后开始做Aut ...
- 动态渲染可编辑单元格的Table
一.问题描述 问题是这样的,后台传了xArr = [x1, x2,...,xn]和yArr = [y1, y2, ..yn]两个数组,前端要渲染出表格并且可以填写每个单元格的值,然后按照一定数据结构保 ...
- ABAP 动态内表添加单元格颜色字段
*动态内表alv显示时要求某些单元格显示颜色 *wa_fldcat-datatype不能添加LVC_T_SCOL类型,在创建好内表之后,再添加颜色列. DATA: wa_fldcat TYPE lvc ...
- IOS开发技巧快速生成二维码
随着移动互联网的发展,二维码应用非常普遍,各大商场,饭店,水果店 基本都有二维码的身影,那么ios中怎么生成二维码呢? 下面的的程序演示了快速生成二维码的方法: 在ios里面要生成二维码,需要借助一个 ...
- iOS开发-UITextView根据内容自适应高度
UITextView作为内容文本输入区域,有的时候我们需要根据内容动态改变文本区域的高度,效果如下: 定义UITextView,实现UITextViewDelegate: -(UITextView * ...
- ios开发-确定/自适应textView的高度
昨天在做学院客户端的时候,随手clean了下项目. 不过xcode又闹脾气了,textview里面的字体大小居然在真机运行的时候普遍小了2号.. 这下蛋疼了.应该我项目里面textview的frame ...
- UITableViewcell autolayout下动态高度
项目中最经常使用的一个UI就是UITableView了.iOS7.8进一步优化了复用机制,用起来相当爽.配合Autolayout,适配工作减轻了非常多. 曾经做适配工作都是在heightForRow里 ...
随机推荐
- Windows Time Client
Timezone: UTC Coordinated Universal Time ====Perform by Local / administrator must,configure Time se ...
- ubuntu12.04 Qt WebKit编译
转载自:http://my.oschina.net/u/257674/blog/167050 官方文档: http://trac.webkit.org/wiki/BuildingQtOnLinux#D ...
- tomcat:tomcat的OutOfMemoryError解决
最近在熟悉一个开发了有几年的项目,需要把数据库从mysql移植到oracle,首先把jdbc的连接指向 mysql,打包放到tomcat里面,可以跑起来,没有问题,可是当把jdbc连接指向oracle ...
- 教主泡嫦娥(RQNOJ 595)
题目描述 [问题背景] 2012年12月21日下午3点14分35秒,全世界各国的总统以及领导人都已经汇聚在中国的方舟上. 但也有很多百姓平民想搭乘方舟,毕竟他们不想就这么离开世界,所以他们决定要么登上 ...
- Spring 对象的声明与注入
1.怎么把一个对象该过过交给Spring管理? 1)通过xml中配置<bean>节点来声明 2)通过Spring注解来声明,如:@Service.@Repository.@Componen ...
- Backbone Collection 源码简谈
一切由一个例子引发: var Man=Backbone.Model.extend({ initilize:function(){ this.bind('change:name',function(){ ...
- pool.map的第二个参数想传入多个咋整?
from functools import partial from multiprocessing import Pool as ThreadPool pageurls=[] if maxpage: ...
- [转]C++ 指针和引用
转自http://www.cnblogs.com/tangxiaobo199181/ 作者:算法生活 微信公众号:算法生活 出处:http://www.cnblogs.com/tangxiaobo19 ...
- 一次处理CentOS服务器被攻击往外发广播包
情况是这样:我们在某地托管的一台linux服务器,突然接到机房电话说是我们机器将整个IDC网络搞瘫了.外部机器没法访问IDC. 挂掉电话后:我就开始考虑,托管机器的机房是有硬防的,我本身一台机器怎么 ...
- ITerms2在mac系统下的安装和配色,并和go2shell关联
官网下载并安装 拖到应用文件夹使其在应用中展示 熟悉快捷键 无鼠标复制: cmd+f:查找首字母,再按tab向右选择词汇,按shift+tab向左选择词汇 分屏 cmd+d:垂直分屏 cmd+shif ...