一,效果图。

二,工程图。

三,代码。

RootViewController.h

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController
<UITableViewDataSource,UITableViewDelegate>
{
UITableView* myTableView;
NSDictionary *dataDic;
}
@property (nonatomic , retain)NSArray* titleArray;
@property (nonatomic , retain)NSArray* contentArray; @end

RootViewController.m

#import "RootViewController.h"
#import "RTLabel.h" @interface RootViewController () @end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view. self.title=@"RTLabel"; [self initContentView]; }
- (void)initContentView
{
//初始化标题
self.titleArray = [NSArray arrayWithObjects:
@"姓名:",
@"年绩:",
@"家乡:",
@"介绍:",
nil]; self.contentArray = [NSArray arrayWithObjects:
@"李华",
@"23",
@"河北",
@"我叫李华,今年23,来自河北,希望大家多多欢迎,喜欢计算机,来自河北的一个学校.111111111111111111111111111",nil]; //添加列表
myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.bounds.size.height) style:UITableViewStylePlain];
[myTableView setDelegate:self];
[myTableView setDataSource:self];
[myTableView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:myTableView];
}
#pragma mark - tableview delegate and dataSoucre - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* identifier = @"cell"; UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone]; [cell setBackgroundColor:[UIColor clearColor]]; //cell白色背景
UIImage* bcImg = [UIImage imageNamed:@"label"];
UIImageView* bcImgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(cell.frame), CGRectGetHeight(cell.contentView.frame))];
[bcImgV setImage:bcImg];
[cell.contentView addSubview:bcImgV]; //cell中的内容
RTLabel* nameLabel = [[RTLabel alloc] initWithFrame:CGRectMake( 10, 15, 300 , 15)];
[nameLabel setText:[NSString stringWithFormat:@"<font face='Helvetica' size=14 color=orange> %@ </font> <font face=AmericanTypewriter size=14 color=gray> %@ </font> ",[self.titleArray objectAtIndex:indexPath.row],[self.contentArray objectAtIndex:indexPath.row]]];
CGSize optimumSize = [nameLabel optimumSize];
[nameLabel setFrame:CGRectMake( 10, 15, 300 , optimumSize.height)];
[nameLabel setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:nameLabel]; [bcImgV setFrame:CGRectMake(0, 0, 320, 30 + optimumSize.height)];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
RTLabel *label = [[RTLabel alloc] initWithFrame:CGRectMake(10,10,300,100)];
//[label setFont:[UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:20]];
[label setParagraphReplacement:@""];
[label setText:[NSString stringWithFormat:@"<font face='Helvetica' size=14 color=orange> %@ </font> <font face=AmericanTypewriter size=14 color=gray> %@ </font> ",[self.titleArray objectAtIndex:indexPath.row],[self.contentArray objectAtIndex:indexPath.row]]];
CGSize optimumSize = [label optimumSize]; return 30 + optimumSize.height;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.titleArray.count;
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

【代码笔记】iOS-cell自动变化大小的更多相关文章

  1. UGUI小技巧之Text随文本内容自动变化大小

    看了网上很多帖子,都是说在 Text 上面加上 Content Size Fitter 组件,并将对应的轴向改成 Preferred size 就可以实现 Text 大小随着文本内容自适应,如下图: ...

  2. Qt QLabel 大小随内容自动变化 && 内容填充整个label空间

    图1:label的本身大小 图2:给label设置文字,不做任何别的设置 ui->label->setText(QObject::tr("current font is %1&q ...

  3. Web开发者和设计师必须要知道的 iOS 8 十个变化

    原文出处: mobilexweb   译文出处:罗磊(@罗罗磊磊)   欢迎分享原创到伯乐头条 喜大普奔,喜极而泣,喜当爹,随着iPhone 6和iPhone 6 plus的上市,ios 8终于在上周 ...

  4. 基于MVC4+EasyUI的Web开发框架经验总结(17)--布局和对话框自动适应大小的处理

    在我自己的<Web开发框架>中,用了很多年的EasyUI,最新版本EasyUI为1.4.5,随着版本的更新,其很多功能得到了很大的完善和提高,同时也扩展了一些新的功能,以前在布局和对话框弹 ...

  5. Masonry -- 使用纯代码进行iOS应用的autolayout自适应布局

    简介 简化iOS应用使用纯代码机型自适应布局的工作,使用一种简洁高效的语法替代NSLayoutConstraints. 项目主页: Masonry 最新示例: 点击下载 项目简议: 如果再看到关于纯代 ...

  6. 减小iOS应用程序的大小

    减小iOS应用程序的大小 本文译自:Reducing the size of my App Q: 怎样才能让我的程序安装包小一点,让程序的下载和安装更快速? A: 本文收集了一些减小程序安装包大小的相 ...

  7. 笔记-iOS 视图控制器转场详解(上)

    这是一篇长文,详细讲解了视图控制器转场的方方面面,配有详细的示意图和代码,为了使得文章在微信公众号中易于阅读,seedante 辛苦将大量长篇代码用截图的方式呈现,另外作者也在 Github 上附上了 ...

  8. 在iOS上自动检测内存泄露

    手机设备的内存是一个共享资源.应用程序可能会不当的耗尽内存.崩溃,或者遭遇大幅度的性能降低. Facebook iOS客户端有很多功能,并且它们共享同一块内存空间.如果任何特定的功能消耗过多的内存,就 ...

  9. 根据Facebook内存的管理使用,浅谈在iOS上自动检测内存泄漏问题

    分装库下载:https://github.com/facebook/FBMemoryProfiler FBMemoryProfiler类库使用教程:http://ifujun.com/fbmemory ...

随机推荐

  1. Storm Trident状态

    Trident中有对状态数据进行读取和写入操作的一流抽象工具.状态既可以保存在拓扑内部,比如保存在内容中并由HDFS存储,也可以通过外部存储(比如Memcached或Cassandra)存储在数据库中 ...

  2. [微信小程序]——bug记录

    记录日常开发小程序遇到的一些小问题: input 输入框(unfixed) 描述:输入框focus的时候,placeholder会往上面跳动一下 当 scroll-view 遇上 fixed 描述:给 ...

  3. 【wireshark】开发环境搭建

    1. 引言 本文相关内容可参考Wireshark开发指南第2章”Quick Setup” 要对wireshark代码进行修改,除了下文介绍的lua插件的方式以外,都需要对wirehshark源码进行编 ...

  4. JS - ECMAScript2015(ES6)新特性

    友情提示:本文仅mark几个常用的新特性,详细请参见:ES6入门 - ryf: 碎片 var VS let VS const var:声明全局变量, let:声明块级变量,即局部变量 const:声明 ...

  5. (转)LINUX CENTOS7下安装PYTHON

    LINUX CENTOS7下安装PYTHON 原文:http://www.cnblogs.com/lclq/p/5620196.html Posted on 2016-06-27 14:58 南宫羽香 ...

  6. Java之IO(十一)BufferedReader和BufferedWriter

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7074488.html 1.前言 按照字节流的顺序一样,字符流也提供了缓冲字符流,与字节流不同,Java虽然提供了 ...

  7. 《Mysql技术内幕,Innodb存储引擎》——索引与算法

    B+树 B+树中,所有记录节点都按照键值的大小顺序放在同一层叶子节点,各个叶子节点指针进行连接. 图中指针是单向的,但是书上的图是双向的,而且旋转应该也是双向才能完成) B+树插入处理 Leaf Pa ...

  8. 面向UI编程框架:ui.js框架思路详细设计

    由于上一次的灵光一闪,萌生了对面向UI编程的思想实现.经过一段时间的考虑和设计,现在将思想和具体细节记录下来: 具体思路描述: 在UI.config文件中,配置所有参数,比如页面模板.所有组件.组件控 ...

  9. Tomcat 配置上传文件到项目外的路径

    使用 Tomcat 作为服务器的时候,将上传文件保存在项目路径下,每次重启服务或者打成 war 包的时候很容易丢失上传的文件,于是我们配置 Tomcat 把文件保存到项目外的其他磁盘路径: 1. 打开 ...

  10. JBoss 实战(1)

    转自:https://www.cnblogs.com/aiwz/p/6154594.html JBOSS的诞生 1998年,在硅谷SUN公司的SAP实验室,一个年轻人正坐在电脑前面思考,然后写着什么东 ...