一,效果图。

二,工程图。

三,代码。

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. [JavaScript] 时间戳格式化为yyyy-MM-dd日期

    function formateDate(timestamp){ var date = new Date(timestamp); var y = 1900+date.getYear(); var m ...

  2. 在node中使用promise上传图片到七牛云

    为了分摊个人服务器压力.提升图片下载上传的速度,使用七牛云保存用户上传的图片. 后台基于express搭建的,上传使用七牛云第三方nodejs-sdk.由于七牛云上传图片只能单个进行,并且考虑到上传完 ...

  3. [Swift实际操作]七、常见概念-(2)点CGPoint和变形CGAffineTransform的使用

    本文将为你演示点对象CGPoint的使用,其中CG表示来自CoreGraphic(核心图形)这个跨平台框架 首先导入需要使用的两个框架第一个框架表示界面工具框架第二个框架表示核心绘图和动画框架 imp ...

  4. C++命名空间、函数重载、缺省参数、内联函数、引用

    一 .C++入门 1.C++关键字 2.命名空间 3.C++输入&输出 4.缺省参数 5.函数重载 6.引用 7.内联函数 8.auto关键字 9.基于范围的for循环 10.指针空值null ...

  5. POJ 1050

    #include <stdio.h> #include <string.h> #define mt 101 int main() { int a[mt][mt]; int st ...

  6. Numpy 数组和dtype的一个使用误区

    首先自定义三种类型(如下代码1-3行),第一行使用scalar type,第2,3行使用Structured type. 提出问题:第5,7行同为创建数组,为什么第5行能work,而第7行会raise ...

  7. MySql登陆密码忘记-解决方案

    方法一:MySQL提供跳过访问控制的命令行参数,通过在命令行以此命令启动MySQL服务器: safe_mysqld --skip-grant-tables& 即可跳过MySQL的访问控制,任何 ...

  8. Mysql 5.7版本安装:mysql 服务无法启动。

    一.解压文件 下载好MySQL后,解压到D盘下,也可以根据个人喜好解压在其他盘符的路径下,解压后的路径是:D:\mysql-5.7.17-winx64.解压好后不要太兴奋,需要配置默认文件呢! 二. ...

  9. 暴力攻击 PHP 脚本 初探

    考虑下面的HTML表单: CODE: <form action="http://example.org/login.php" method="POST"& ...

  10. Maven 学习笔记(二)

    项目最近开始使用maven去管理项目啦,说真的对于maven是一窍不通啊,今天和同事在回家的路上聊天的时候同事说他去第一家公司面试的时候人家问他 maven 怎么打包,当时我就懵逼了,因为我也不知道啊 ...