【代码笔记】iOS-cell自动变化大小
一,效果图。

二,工程图。

三,代码。
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自动变化大小的更多相关文章
- UGUI小技巧之Text随文本内容自动变化大小
看了网上很多帖子,都是说在 Text 上面加上 Content Size Fitter 组件,并将对应的轴向改成 Preferred size 就可以实现 Text 大小随着文本内容自适应,如下图: ...
- Qt QLabel 大小随内容自动变化 && 内容填充整个label空间
图1:label的本身大小 图2:给label设置文字,不做任何别的设置 ui->label->setText(QObject::tr("current font is %1&q ...
- Web开发者和设计师必须要知道的 iOS 8 十个变化
原文出处: mobilexweb 译文出处:罗磊(@罗罗磊磊) 欢迎分享原创到伯乐头条 喜大普奔,喜极而泣,喜当爹,随着iPhone 6和iPhone 6 plus的上市,ios 8终于在上周 ...
- 基于MVC4+EasyUI的Web开发框架经验总结(17)--布局和对话框自动适应大小的处理
在我自己的<Web开发框架>中,用了很多年的EasyUI,最新版本EasyUI为1.4.5,随着版本的更新,其很多功能得到了很大的完善和提高,同时也扩展了一些新的功能,以前在布局和对话框弹 ...
- Masonry -- 使用纯代码进行iOS应用的autolayout自适应布局
简介 简化iOS应用使用纯代码机型自适应布局的工作,使用一种简洁高效的语法替代NSLayoutConstraints. 项目主页: Masonry 最新示例: 点击下载 项目简议: 如果再看到关于纯代 ...
- 减小iOS应用程序的大小
减小iOS应用程序的大小 本文译自:Reducing the size of my App Q: 怎样才能让我的程序安装包小一点,让程序的下载和安装更快速? A: 本文收集了一些减小程序安装包大小的相 ...
- 笔记-iOS 视图控制器转场详解(上)
这是一篇长文,详细讲解了视图控制器转场的方方面面,配有详细的示意图和代码,为了使得文章在微信公众号中易于阅读,seedante 辛苦将大量长篇代码用截图的方式呈现,另外作者也在 Github 上附上了 ...
- 在iOS上自动检测内存泄露
手机设备的内存是一个共享资源.应用程序可能会不当的耗尽内存.崩溃,或者遭遇大幅度的性能降低. Facebook iOS客户端有很多功能,并且它们共享同一块内存空间.如果任何特定的功能消耗过多的内存,就 ...
- 根据Facebook内存的管理使用,浅谈在iOS上自动检测内存泄漏问题
分装库下载:https://github.com/facebook/FBMemoryProfiler FBMemoryProfiler类库使用教程:http://ifujun.com/fbmemory ...
随机推荐
- Storm的并行度
在Storm集群中,运行Topolopy的实体有三个:工作进程,executor(线程),task(任务),下图可以形象的说明他们之间的关系. 工作进程 Storm集群中的一台机器会为一个或则多个To ...
- Jmeter之Bean shell使用
转载地址:http://www.cnblogs.com/puresoul/p/4915350.html 一.什么是Bean Shell BeanShell是一种完全符合Java语法规范的脚本语言,并且 ...
- Centos下用yum命令按照jdk
一.查看是否已经安装了JDK #查看本机是否已经安装了jdk $ sudo yum list installed | grep java java--openjdk.x86_64 :.el7_6 @u ...
- 安装Hive-0.10.0-CDH4.5.0所遇异常
Note: 虚拟机访问Win7中mysql(root用户+密码) hive出现异常1: FAILED: Error in metadata: java.lang.RuntimeException: U ...
- (转)inspect — Inspect live objects
原文:https://docs.python.org/3/library/inspect.html 中文:https://www.rddoc.com/doc/Python/3.6.0/zh/libra ...
- ASP.NET:EntityFramework实现Session
ASP.NET默认的InProc模式的Session既浪费内存又在网站重启时存在数据丢失问题,SQLServer模式的Session只支持SQL Server又需要命令行配置.使用EntityFram ...
- Mina的客户端
(一) package client; import java.net.InetSocketAddress; import java.nio.charset.Charset; import org.a ...
- 一分钟让你学会使用Android AsyncTask
AsyncTask相信大多数朋友对它的用法都已经非常熟悉,这里记录一下主要是献给那些刚刚接触的Android 或者AsyncTask的同学们,高手请绕道. AsyncTask类是Android1.5版 ...
- 关于Class的invokeDynamic指令
(1)java7之Special Methods (2)invokedynamic指令 https://www.cnblogs.com/wade-luffy/p/6058087.html public ...
- Vue图片懒加载之lazyload插件使用
当内容没有加载完的时候,用户体验不是很好,这时候,可以使用lazyload这个插件,提升用户体验,使用方法特别简单易用 一. vue lazyload插件: 插件地址:https://github.c ...