步骤一、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. 1: //
  1. 2: // NewsCell.h
  1. 3: // M04P20-新闻
  1. 4: //
  1. 5: // Created by 张泽阳 on 4/26/15.
  1. 6: // Copyright (c) 2015 张泽阳. All rights reserved.
  1. 7: //
  1. 8:  
  1. 9: #import <UIKit/UIKit.h>
  1. 10: @class News;
  1. 11: @interface NewsCell : UITableViewCell
  1. 12:  
  1. 13: @property (nonatomic,strong) UILabel* newsTitleLabel;
  1. 14: @property (nonatomic,strong) UILabel* newsAuthorLabel;
  1. 15: @property (nonatomic,strong) UILabel* newsCommentsLabel;
  1. 16: @property (nonatomic,strong) UIImageView* pictureImageView;
  1. 17: -(void)setContentData:(News*)news;
  1. 18: +(instancetype)newsCellWithTableView:(UITableView*)tableView;
  1. 19: @end
  1. 20:  
  1. 21:  
  1. 22: //
  1. 23: // NewsCell.m
  1. 24: // M04P20-新闻
  1. 25: //
  1. 26: // Created by 张泽阳 on 4/26/15.
  1. 27: // Copyright (c) 2015 张泽阳. All rights reserved.
  1. 28: //
  1. 29:  
  1. 30: #import "NewsCell.h"
  1. 31: #import "News.h"
  1. 32: #import "Masonry.h"
  1. 33: @implementation NewsCell
  1. 34:  
  1. 35: /**
  1. 36: * 懒加载
  1. 37: */
  1. 38: -(UILabel *)newsCommentsLabel{
  1. 39: if (!_newsCommentsLabel) {
  1. 40: _newsCommentsLabel = [[UILabel alloc]init];
  1. 41: }
  1. 42: return _newsCommentsLabel;
  1. 43: }
  1. 44: -(UILabel *)newsTitleLabel{
  1. 45: if (!_newsTitleLabel) {
  1. 46: _newsTitleLabel = [[UILabel alloc]init];
  1. 47:
  1. 48: }
  1. 49: return _newsTitleLabel;
  1. 50: }
  1. 51: -(UILabel *)newsAuthorLabel{
  1. 52: if (!_newsAuthorLabel) {
  1. 53: _newsAuthorLabel = [[UILabel alloc]init];
  1. 54: [self.contentView addSubview:_newsAuthorLabel];
  1. 55: }
  1. 56: return _newsAuthorLabel;
  1. 57: }
  1. 58: -(UIImageView *)pictureImageView{
  1. 59: if (!_pictureImageView) {
  1. 60: _pictureImageView = [[UIImageView alloc]init];
  1. 61: }
  1. 62: return _pictureImageView;
  1. 63: }
  1. 64: /**
  1. 65: * 便利构造器
  1. 66: */
  1. 67: +(instancetype)newsCellWithTableView:(UITableView *)tableView{
  1. 68: static NSString* ID = @"news";
  1. 69: NewsCell* cell = [tableView dequeueReusableCellWithIdentifier:ID];
  1. 70: if (!cell) {
  1. 71: cell = [[NewsCell alloc]initWIthTableView:tableView andID:ID];
  1. 72: }
  1. 73:
  1. 74: [cell setNeedsLayout];
  1. 75: [cell layoutIfNeeded];
  1. 76: return cell;
  1. 77: }
  1. 78: /**
  1. 79: * init中设置相对固定的数据
  1. 80: *
  1. 81: */
  1. 82: -(instancetype)initWIthTableView:(UITableView*)tablview andID:(NSString*)ID{
  1. 83: // static NSString* ID = @"news";
  1. 84: self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];//别忘了super init
  1. 85: [self.contentView addSubview:self.newsTitleLabel];
  1. 86: [self.contentView addSubview:self.newsAuthorLabel];
  1. 87: [self.contentView addSubview:self.pictureImageView];
  1. 88: [self.contentView addSubview:self.newsCommentsLabel];
  1. 89:
  1. 90: self.newsCommentsLabel.textColor = [UIColor grayColor];
  1. 91: self.newsCommentsLabel.font = [UIFont systemFontOfSize:12];
  1. 92: [self.newsCommentsLabel setHighlightedTextColor:[UIColor whiteColor]];
  1. 93: self.newsAuthorLabel.textColor = [UIColor grayColor];
  1. 94: self.newsAuthorLabel.font = [UIFont systemFontOfSize:12];
  1. 95: [self.newsAuthorLabel setHighlightedTextColor:[UIColor whiteColor]];
  1. 96:
  1. 97: self.newsTitleLabel.font = [UIFont boldSystemFontOfSize:15];
  1. 98: self.newsTitleLabel.numberOfLines = 0;
  1. 99:
  1. 100: [self.newsTitleLabel setHighlightedTextColor:[UIColor whiteColor]];
  1. 101: self.pictureImageView.contentMode = UIViewContentModeScaleAspectFit;
  1. 102:
  1. 103: return self;
  1. 104: }
  1. 105: /**
  1. 106: * 设置相对动态的数据
  1. 107: */
  1. 108: -(void)setContentData:(News *)news{
  1. 109: self.newsTitleLabel.text = news.title;
  1. 110: self.newsAuthorLabel.text = news.author;
  1. 111: self.newsCommentsLabel.text = [NSString stringWithFormat:@"评论:%d",news.comments];
  1. 112: self.pictureImageView.image = [UIImage imageNamed:news.icon];
  1. 113: }
  1. 114: /**
  1. 115: * 设置布局 记得调用super
  1. 116: */
  1. 117: -(void)layoutSubviews{
  1. 118: [super layoutSubviews];
  1. 119: [self.newsTitleLabel mas_makeConstraints:^(MASConstraintMaker *make){
  1. 120: make.top.equalTo(self.contentView.mas_top).mas_offset(8); ///offset???
  1. 121: make.left.equalTo(self.contentView.mas_left).mas_offset(8);
  1. 122: make.right.equalTo(self.pictureImageView.mas_left).mas_offset(-8);
  1. 123: }];
  1. 124:
  1. 125: [self.newsAuthorLabel mas_makeConstraints:^(MASConstraintMaker *make){
  1. 126: make.left.equalTo(self.newsTitleLabel);
  1. 127: make.top.greaterThanOrEqualTo(self.newsTitleLabel.mas_bottom).mas_offset(8);
  1. 128: make.bottom.equalTo(self.contentView.mas_bottom).mas_offset(-8);
  1. 129: }];
  1. 130:
  1. 131: [self.newsCommentsLabel mas_makeConstraints:^(MASConstraintMaker *make){
  1. 132: make.top.equalTo(self.newsAuthorLabel.mas_top);
  1. 133: make.right.equalTo(self.pictureImageView.mas_left).mas_offset(-8);
  1. 134: }];
  1. 135:
  1. 136: [self.pictureImageView mas_makeConstraints:^(MASConstraintMaker *make){
  1. 137: make.top.equalTo(self.contentView.mas_top).mas_offset(8);
  1. 138: make.right.equalTo(self.contentView.mas_right).mas_offset(-8);
  1. 139: make.bottom.equalTo(self.contentView.mas_bottom).mas_offset(-8);
  1. 140: make.width.mas_equalTo(120);
  1. 141: }];
  1. 142: }
  1. 143: /**
  1. 144: * 单元格被选中/取消选中时的表现
  1. 145: */
  1. 146: -(void)setSelected:(BOOL)selected animated:(BOOL)animated{
  1. 147: [super setSelected:selected animated:animated];
  1. 148: // self.newsTitleLabel.textColor = selected?([UIColor whiteColor]):([UIColor blackColor]);
  1. 149: // self.newsAuthorLabel.textColor = selected?([UIColor whiteColor]):([UIColor blackColor]);
  1. 150: // self.newsCommentsLabel.textColor = selected?([UIColor whiteColor]):([UIColor blackColor]);
  1. 151: }
  1. 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. 1: //
  1. 2: // ViewController.h
  1. 3: // M04P20-新闻
  1. 4: //
  1. 5: // Created by 张泽阳 on 4/26/15.
  1. 6: // Copyright (c) 2015 张泽阳. All rights reserved.
  1. 7: //
  1. 8:  
  1. 9: #import <UIKit/UIKit.h>
  1. 10:  
  1. 11: @interface ViewController : UIViewController
  1. 12: @property (nonatomic,strong) NSArray* newses;
  1. 13:  
  1. 14: @end
  1. 15:  
  1. 16:  
  1. 17: //
  1. 18: // ViewController.m
  1. 19: // M04P20-新闻
  1. 20: //
  1. 21: // Created by 张泽阳 on 4/26/15.
  1. 22: // Copyright (c) 2015 张泽阳. All rights reserved.
  1. 23: //
  1. 24:  
  1. 25: #import "ViewController.h"
  1. 26: #import "News.h"
  1. 27: #import "NewsCell.h"
  1. 28: #import "NSObject+NSObject_ZZYMODEL.h"
  1. 29: @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
  1. 30: @property (weak, nonatomic) IBOutlet UITableView *tableView;
  1. 31:  
  1. 32: @end
  1. 33:  
  1. 34: @implementation ViewController
  1. 35: -(NSArray *)newses{
  1. 36: if (!_newses) {
  1. 37: _newses = [News modelArrayWithFilename:@"news.plist"];
  1. 38: }
  1. 39: return _newses;
  1. 40: }
  1. 41: - (void)viewDidLoad {
  1. 42: [super viewDidLoad];
  1. 43: self.tableView.estimatedRowHeight = 54;
  1. 44: self.tableView.rowHeight = UITableViewAutomaticDimension;
  1. 45: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidRotate) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
  1. 46: }
  1. 47: -(BOOL)prefersStatusBarHidden{
  1. 48: return YES;
  1. 49: }
  1. 50: -(void)orientationDidRotate{
  1. 51: [self.tableView reloadData];
  1. 52: }
  1. 53: -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  1. 54: return 1;
  1. 55: }
  1. 56:  
  1. 57: -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  1. 58: return self.newses.count;
  1. 59: }
  1. 60:  
  1. 61: -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  1. 62: NewsCell* cell = [NewsCell newsCellWithTableView:tableView];
  1. 63: [cell setContentData:self.newses[indexPath.row]];
  1. 64:
  1. 65:
  1. 66: return cell;
  1. 67:
  1. 68: }
  1. 69:  
  1. 70:  
  1. 71:  
  1. 72:  
  1. 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://itony.me/381.html

http://www.mgenware.com/blog/?p=507

http://blog.163.com/china_uv/blog/static/1171372672014111681232340/

http://codingobjc.com/blog/2014/10/15/shi-yong-autolayoutshi-xian-uitableviewde-celldong-tai-bu-ju-he-ke-bian-xing-gao/index.html

iOS开发——使用Autolayout生成动态高度的TableViewCell单元格的更多相关文章

  1. ios开发不能不知的动态修复bug补丁第三方库JSPatch 使用学习:JSPatch导入、和使用、.js文件传输加解密

    JSPatch ios开发不能不知的动态修复bug补丁第三方库JSPatch 使用学习:JSPatch导入.和使用..js文件传输加解密 ios开发面临审核周期长,修复bug延迟等让人无奈的问题,所以 ...

  2. js如何实现动态点击改变单元格颜色?

    js如何实现动态点击改变单元格颜色? 一.总结 1.通过table的rows属性,遍历表格所有行,然后通过cells属性,遍历每一行中的单元格. 2.遍历的过程中,动态的为每一个单元格定义单击事件,改 ...

  3. 使用Autolayout xib实现动态高度的TableViewCell

    http://my.oschina.net/u/2360693/blog/481236?p={{totalPage}} 创建Xib文件 首先将Cell做好布局,调整到满意的位置和宽度,然后开始做Aut ...

  4. 动态渲染可编辑单元格的Table

    一.问题描述 问题是这样的,后台传了xArr = [x1, x2,...,xn]和yArr = [y1, y2, ..yn]两个数组,前端要渲染出表格并且可以填写每个单元格的值,然后按照一定数据结构保 ...

  5. ABAP 动态内表添加单元格颜色字段

    *动态内表alv显示时要求某些单元格显示颜色 *wa_fldcat-datatype不能添加LVC_T_SCOL类型,在创建好内表之后,再添加颜色列. DATA: wa_fldcat TYPE lvc ...

  6. IOS开发技巧快速生成二维码

    随着移动互联网的发展,二维码应用非常普遍,各大商场,饭店,水果店 基本都有二维码的身影,那么ios中怎么生成二维码呢? 下面的的程序演示了快速生成二维码的方法: 在ios里面要生成二维码,需要借助一个 ...

  7. iOS开发-UITextView根据内容自适应高度

    UITextView作为内容文本输入区域,有的时候我们需要根据内容动态改变文本区域的高度,效果如下: 定义UITextView,实现UITextViewDelegate: -(UITextView * ...

  8. ios开发-确定/自适应textView的高度

    昨天在做学院客户端的时候,随手clean了下项目. 不过xcode又闹脾气了,textview里面的字体大小居然在真机运行的时候普遍小了2号.. 这下蛋疼了.应该我项目里面textview的frame ...

  9. UITableViewcell autolayout下动态高度

    项目中最经常使用的一个UI就是UITableView了.iOS7.8进一步优化了复用机制,用起来相当爽.配合Autolayout,适配工作减轻了非常多. 曾经做适配工作都是在heightForRow里 ...

随机推荐

  1. video视频在结束之后回到初始状态

    目前尝试了两种解决方案,但是方案1在安卓移动端无法生效(猜测是因为移动端安卓启动的是原生的视频播放控件的原因) 方案一: 重新load资源,这种方法比较简洁,但是在安卓下不适用 video.addEv ...

  2. js闭包,原型,作用域等再一次理解

    要理解闭包,原型等,首先要理解作用域 作用域:就是函数在定义的时候创建的,用于寻找使用到的变量的值的一个索引,而他内部的规则是,把函数自身的本地变量放在最前面,把自身的父级函数中的变量放在其次,把再高 ...

  3. DotNETCore 学习笔记 配置

    Configuration var builder = new ConfigurationBuilder(); builder.AddInMemoryCollection(); var config ...

  4. C++学习笔记之——内联函数,引用

    本文为原创作品,转载请注明出处 欢迎关注我的博客:http://blog.csdn.net/hit2015spring和http://www.cnblogs.com/xujianqing/ 作者:晨凫 ...

  5. efi转bios详细说明

    前言 制作好的efi格式的ubuntu15.10系统放到服务器主板上启动不了,于是将其改为bios格式,发现问题解决了,成功登入系统.下面是操作过程的一个记录. 测试环境 目标环境 系统: Ubunt ...

  6. centos6.5 挂载远程目录

    查看nfs程序是否安装: [root@crawler_mv02 ~]# rpm -qa |grep rpcbindrpcbind-0.2.0-13.el6_9.1.x86_64[root@crawle ...

  7. DELPHI用const来提高应用程序在多核多线程下的性能

    来自:http://bbs.csdn.net/topics/330048800 --------------------------------------------------------- 我们 ...

  8. Linux命令之:tr

    1. 用途: tr,translate的简写,主要用于压缩重复字符,删除文件中的控制字符以及进行字符转换操作. 2. 语法: tr [OPTION]... SET1 [SET2] 3. 参数: -s: ...

  9. Django基础之模板

    Django模板系统 官方文档 常用语法 只需要记两种特殊符号: {{  }} 和 {% %} 变量相关的用{{ }},逻辑相关的用{% %}. 变量 {{ 变量名 }} 变量名由字母数字和下划线组成 ...

  10. hdu 1498(最小点覆盖集)

    50 years, 50 colors Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...