步骤一、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://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. SQL性能分析

    MySQL常见瓶颈: CPU:CPU在饱和的时候一般发生在数据装入内存或从磁盘上读取数据的时候. IO:磁盘I/O瓶颈发生在装入数据远大于内存容量的时候. 服务器硬件的性能瓶颈:top.free.io ...

  2. 转:JVM Server与Client运行模式

    转自:http://blog.csdn.net/zhuyijian135757/article/details/38391785 JVM Server模式与client模式启动,最主要的差别在于:-S ...

  3. linux编译动态库 fPIC作用

    在生成动态库时,常常习惯性的加上fPIC选项,fPIC有什么作用和意义,加不加有什么区别,这里做下小结: fPIC的全称是 Position Independent Code, 用于生成位置无关代码. ...

  4. Python基础(2)_if、for、while流程控制

    一.流程控制 1.条件语句 1.1单分支 1.2多分支 条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. a = 5 if a > 2: print('yes') 根据Py ...

  5. 知问前端——日历UI(三)

    datepicker日期选择选项 属性 默认值/类型 说明 minDate 无/对象.字符串或数值 日历中可以选择的最小日期 maxDate 无/对象.字符串或数值 日历中可以选择的最大日期 defa ...

  6. bzoj 2245 费用流

    比较裸 源点连人,每个人连自己的工作,工作连汇,然后因为人的费用是 分度的,且是随工作数非降的,所以我们拆边,源点连到每个人s+1条边 容量是每段的件数,费用是愤怒 /**************** ...

  7. nginx启动脚本(class练习)

    说明:使用类的方式编写程序启动脚本(练习) 1 #!/usr/bin/env python import sys import os from subprocess import Popen,PIPE ...

  8. AngularJS 入门教程

    1. 简介:AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文本展示设计的声明式语言,但要构建WEB应用的话它就显得乏力了.所以我做了一些工作(你也可以觉得是 ...

  9. Solidity 文档--第一章:智能合约入门

    一个简单的智能合约 先从一个非常基础的例子开始,不用担心你现在还一点都不了解,我们将逐步了解到更多的细节. 存储 contract SimpleStorage { uint storedData; f ...

  10. mysql的expain(zz)

    两张表,T1和T2,都只有一个字段,id int.各插入1000条记录,运行如下语句: explain SELECT t1.id,t2.id FROM t1 INNER JOIN t2 ON t1.i ...