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里 ...
随机推荐
- video视频在结束之后回到初始状态
目前尝试了两种解决方案,但是方案1在安卓移动端无法生效(猜测是因为移动端安卓启动的是原生的视频播放控件的原因) 方案一: 重新load资源,这种方法比较简洁,但是在安卓下不适用 video.addEv ...
- js闭包,原型,作用域等再一次理解
要理解闭包,原型等,首先要理解作用域 作用域:就是函数在定义的时候创建的,用于寻找使用到的变量的值的一个索引,而他内部的规则是,把函数自身的本地变量放在最前面,把自身的父级函数中的变量放在其次,把再高 ...
- DotNETCore 学习笔记 配置
Configuration var builder = new ConfigurationBuilder(); builder.AddInMemoryCollection(); var config ...
- C++学习笔记之——内联函数,引用
本文为原创作品,转载请注明出处 欢迎关注我的博客:http://blog.csdn.net/hit2015spring和http://www.cnblogs.com/xujianqing/ 作者:晨凫 ...
- efi转bios详细说明
前言 制作好的efi格式的ubuntu15.10系统放到服务器主板上启动不了,于是将其改为bios格式,发现问题解决了,成功登入系统.下面是操作过程的一个记录. 测试环境 目标环境 系统: Ubunt ...
- centos6.5 挂载远程目录
查看nfs程序是否安装: [root@crawler_mv02 ~]# rpm -qa |grep rpcbindrpcbind-0.2.0-13.el6_9.1.x86_64[root@crawle ...
- DELPHI用const来提高应用程序在多核多线程下的性能
来自:http://bbs.csdn.net/topics/330048800 --------------------------------------------------------- 我们 ...
- Linux命令之:tr
1. 用途: tr,translate的简写,主要用于压缩重复字符,删除文件中的控制字符以及进行字符转换操作. 2. 语法: tr [OPTION]... SET1 [SET2] 3. 参数: -s: ...
- Django基础之模板
Django模板系统 官方文档 常用语法 只需要记两种特殊符号: {{ }} 和 {% %} 变量相关的用{{ }},逻辑相关的用{% %}. 变量 {{ 变量名 }} 变量名由字母数字和下划线组成 ...
- hdu 1498(最小点覆盖集)
50 years, 50 colors Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...