效果:

描述:表视图中生成多个不同的cell,cell的高度跟文字内容的多少有关

要求:需要自己在网上下载一个plis文件,然后修改两个标题

一 : 创建工程文件UIAutomaticCellHeightPractce-11

二 : 这里使用手动释放,所以需要修改工程文件Automatic Reference Counting 修改为NO

三 : 修改引用计数方式

四 : 释放适量变量,以及对父集进行释放,并让自动释放self.window(autorelease)

五 : 导入.plist文件(直接拖进工程文件)

六 : 工程文件

七 :代码区(采用MVC的设计模式)

模型-视图-控制器(Model-View-Controller,MVC)

AppDelegate.h

 #import <UIKit/UIKit.h>

 @interface AppDelegate : UIResponder <UIApplicationDelegate>

 @property (retain, nonatomic) UIWindow *window;

 @end

AppDelegate.m

 #import "AppDelegate.h"
#import "RootTableViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (void)dealloc
{
self.window = nil; [super dealloc];
} - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
RootTableViewController *rootVC = [[RootTableViewController alloc] init]; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootVC]; self.window.rootViewController = navigationController; [rootVC release]; [navigationController release]; self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
} @end

RootTableViewController.h

 #import <UIKit/UIKit.h>

 @interface RootTableViewController : UITableViewController

 @end

RootTableViewController.m

 #import "RootTableViewController.h"
#import "CellViewModel.h"
#import "CellTableViewCell.h"
#define kCellTabelViewCell @"cell" @interface RootTableViewController () // 创建公用数组
@property (nonatomic , retain) NSMutableArray *dataSourceArr; @end @implementation RootTableViewController - (void)dealloc
{
self.dataSourceArr = nil; [super dealloc];
} - (void)viewDidLoad {
[super viewDidLoad]; [self readDataFromNewsData]; // 注册CellTableView
[self.tableView registerClass:[CellTableViewCell class] forCellReuseIdentifier:kCellTabelViewCell]; // Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO; // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
} #pragma mark - 给dataSourceArr添加懒加载方法
-(NSMutableArray *)dataSourceArr { if (!_dataSourceArr) { self.dataSourceArr = [NSMutableArray arrayWithCapacity:];
} return [[_dataSourceArr retain] autorelease];
} #pragma mark - 读取数据
-(void)readDataFromNewsData { // NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0]; // 创建文件路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"NewsData.plist" ofType:nil]; // 根据路径将文件中的内容取出来存储到字典中
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:filePath]; // 根据key值取出对应的value值,这里假设要查找的文件内容为title,summary
NSArray *arr = dic[@"news"]; // NSLog(@"%@" , arr); for (NSDictionary *d in arr) { // 创建一个CellViewModel对象存储数据
CellViewModel *dataBase = [[CellViewModel alloc] init]; // 使用kvc的方法给CellViewModel对象赋值 /*
2)代码说明: KVC key valued coding 键值编码 1)使用KVC间接修改对象属性时,系统会自动判断对象属性的类型,并完成转换。如该程序中的“23”. 2)KVC按照键值路径取值时,如果对象不包含指定的键值,会自动进入对象内部,查找对象属性
*/ [dataBase setValuesForKeysWithDictionary:d]; // NSLog(@"%@" , dataBase); // 将存储到CellViewModel对象的数据放入数组
[self.dataSourceArr addObject:dataBase]; // NSLog(@"%@" , _dataSourceArr); // 释放
[dataBase release];
}
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark - Table view data source (表视图数据源) - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Potentially incomplete method implementation.
// Return the number of sections. // 创建一个分区
return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete method implementation.
// Return the number of rows in the section. // 分区中成员个数
return self.dataSourceArr.count;
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // 根据下标取出相对应的cell
CellViewModel *cellModel = self.dataSourceArr[indexPath.row]; return [CellTableViewCell cellHeight:cellModel];
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 使用据有identifier标示的方法,在重用池中取值
CellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellTabelViewCell forIndexPath:indexPath]; // 根据数组下标取出对应的CellViewModel类型的数据
CellViewModel *cellModel = self.dataSourceArr[indexPath.row]; // 将CellViewModel对象中的值传入CellTableViewCell类型对象
[cell passValue:cellModel]; return cell;
} @end

CellViewModel.h

 #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> // Foundationg框架下无法使用UIKit框架下的内容,在这里引入UIKit框架; 注意引入系统文件使用<> @interface CellViewModel : NSObject // 创建控件,这里空间名称要与要取出的数据的key相同,这样使用kvc赋值的时候才能成功
@property (nonatomic , copy) NSString *title; @property (nonatomic , copy) NSString *summary; @end

CellViewModel.m

 #import "CellViewModel.h"

 @implementation CellViewModel

 - (void)dealloc
{
self.title = nil; self.summary = nil; [super dealloc];
} // 防止未找到对应的key值而crash,需要写一下下面的对象
-(void)setValue:(id)value forUndefinedKey:(NSString *)key { // 里面可以什么都不写
// NSLog(@"%@ -- %@" , key , value);
}
@end

CellTableViewCell.h

 #import <UIKit/UIKit.h>
@class CellViewModel; @interface CellTableViewCell : UITableViewCell #pragma mark - 创建一个类返回cell的高度
+ (CGFloat)cellHeight:(CellViewModel *)cellViewModel; #pragma mark - 创建一个接口,让外部可以通过这个接口传值
-(void)passValue:(CellViewModel *)cellViewModel; @end

CellTableViewCell.m

 #import "CellTableViewCell.h"
#import "CellViewModel.h" @interface CellTableViewCell () // 创建标题Label视图
@property (nonatomic , retain) UILabel *titleLabel; // 创建摘要Label视图
@property (nonatomic , retain) UILabel *summaryLabel; @end @implementation CellTableViewCell - (void)dealloc
{
self.titleLabel = nil; self.summaryLabel = nil; [super dealloc];
} #pragma mark - 创建属性的懒加载方式
// 懒加载方式的创建步骤
// 1. 必须有属性
// 2. 重写属性的getter方法,如果为空那么就创建, 如果有就直接使用
// 好处: 1. 真正使用的时候采取创建它,有效的降低了内存 2. 通过懒加载能够有效的分隔代码块 -(UILabel *)titleLabel { if (!_titleLabel) { self.titleLabel = [[[UILabel alloc] initWithFrame:CGRectMake(, , , )] autorelease]; // 此处最好不要使用self.的方法,因为self.属于getter方法,getter方法中不能套用getter方法,不然就会无限调用
// 设置背景色用于观察控件位置
// _titleLabel.backgroundColor = [UIColor redColor];
}
// 安全释放
return [[_titleLabel retain] autorelease];
} -(UILabel *)summaryLabel { if (!_summaryLabel) {
self.summaryLabel = [[[UILabel alloc] initWithFrame:CGRectMake(, , , )] autorelease]; _summaryLabel.numberOfLines = ; // 行数设置为0,指的就是文字内容自动适应宽度(自动换行); // 设置背景色用于观察控件位置
// _summaryLabel.backgroundColor = [UIColor redColor]; // 设置label文字大小
_summaryLabel.font = [UIFont systemFontOfSize:15.0];
} // 安全释放
return [[_summaryLabel retain] autorelease];
} #pragma mark - 创建一个类返回summary文字内容的总高度
+ (CGFloat)summaryHeight:(CellViewModel *)cellViewModel { // CGRect rect = [cellViewModel.summary boundingRectWithSize:<#(CGSize)#> options:<#(NSStringDrawingOptions)#> attributes:<#(NSDictionary *)#> context:<#(NSStringDrawingContext *)#>]
//
// return rect.size.height;
// 上面方法可以返回文字的宽度高度: 需要提供一个CGSize, NSStringDrawingOptions, NSDictionary对象
// 1. 绘制文本大小,这里要和summary一样宽,但是到不不用管
CGSize contentSize = CGSizeMake(, ); // 这里只需提供宽度就可以了 // 2. 设置文本绘制标准(使用系统提供) // 3. 设置文字属性(注意: 文字的大小要和summaryLabel设置的保持一致)
NSDictionary *dic = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:15.0]}; // 返回文本rect
CGRect rect = [cellViewModel.summary boundingRectWithSize:contentSize options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil]; return rect.size.height;
} #pragma mark - 创建一个类返回cell的高度
+ (CGFloat)cellHeight:(CellViewModel *)cellViewModel { // self在类方法中使用,代表的就是类, 在对象方法中代表的就是对象
return + + + [self summaryHeight:cellViewModel];
} #pragma mark - 创建一个接口,让外部可以通过这个接口传值
-(void)passValue:(CellViewModel *)cellViewModel { // 将model中的值赋值给titleLabel
self.titleLabel.text = cellViewModel.title; // 将model中的值赋值给summaryLabel
self.summaryLabel.text = cellViewModel.summary; // 让_summary的高度变化适应文字内容
// _summaryLabel.frame = CGRectMake(10, 50, [[self class] summaryHeight:cellViewModel], 300); // 在对象方法中由于self代表的是对象,所以要进行一下转换
// [self class] 返回一个类
CGFloat height = [[self class] summaryHeight:cellViewModel]; CGRect sFrame = self.summaryLabel.frame; sFrame.size.height = height; self.summaryLabel.frame = sFrame;
} #pragma mark - 重写初始化将titleLabel和summaryLabel添加到表视图的contentView上
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { // 判断父类是否初始化
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { [self.contentView addSubview:self.titleLabel]; // 这里必须使用self.的方式调用属性,不能使用_的方式 [self.contentView addSubview:self.summaryLabel];
} return self;
} - (void)awakeFromNib {
// Initialization code
} - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated]; // Configure the view for the selected state
} @end

cell高度自动适应文章内容的更多相关文章

  1. 如何给wordpress首页自动显示文章内容的第一个图片

    敏捷个人手机应用中使用到的数据来源于wordpress中,因为自己写的页面,所以可以自己写代码获取文章内容的第一个图片作为文章缩略图来显示,这样用户看到首页时图文并茂,感觉会好一些. 现在后台简单的使 ...

  2. wordpress调用文章摘要,若无摘要则自动截取文章内容字数做为摘要

    以下是调用指定分类文章列表的一个方法,作者如果有填写文章摘要则直接调用摘要:如果文章摘要忘记写了则自动截取文章内容字数做为摘要.这个方法也适用于调用description标签 <ul> & ...

  3. iOS中Cell高度如何能够自动适应需要显示的内容

    本文的代码例子 : "Cell行高自适应.zip" http://vdisk.weibo.com/s/Gb9Mt 下面我们来看看代码.我需要一个第三方库EGO异步下载.addtio ...

  4. 使用事件捕获实时捕获img是否加载完毕, 实现iframe内容高度自动适应

    如何判断在html中图片加载完毕呢? 给img图片加onload事件呗. 如何判断一个界面中所有的图片加载完毕呢? 给所有的图片加上onload事件呗. 如果有1000张图片那要怎么绑定事件呢? 我们 ...

  5. wordpress 为文章内容添加自动过滤,例如为出站链接添加nofollow,也可以将淘宝客链接转换。。

    做seo的都明白,反向链接对与网站的优化有着很重要的作用,是搜索引擎给网站排名的一个重要因素.为了添加反向链接,SEO作弊者会在论坛和博客等大量发布带无关链接的 内容.这些垃圾链接的存在给搜索引擎对网 ...

  6. Masonry 布局 cell 高度适应的一种方案(实现类似朋友圈简单布局)

    来源:伯乐在线 - 夏天然后 链接:http://ios.jobbole.com/89298/ 点击 → 申请加入伯乐在线专栏作者 前言: 我模仿的是微博的布局所以也就没有 评论动态刷新cell. 1 ...

  7. iOS开发之多种Cell高度自适应实现方案的UI流畅度分析

    本篇博客的主题是关于UI操作流畅度优化的一篇博客,我们以TableView中填充多个根据内容自适应高度的Cell来作为本篇博客的使用场景.当然Cell高度的自适应网上的解决方案是铺天盖地呢,今天我们的 ...

  8. 纯CSS实现侧边栏/分栏高度自动相等

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=694 一.为何要分栏高 ...

  9. 转:iOS开发之多种Cell高度自适应实现方案的UI流畅度分析

    本篇博客的主题是关于UI操作流畅度优化的一篇博客,我们以TableView中填充多个根据内容自适应高度的Cell来作为本篇博客的使用场景.当然Cell高度的自适应网上的解决方案是铺天盖地呢,今天我们的 ...

随机推荐

  1. 优化JavaScript脚本的性能

    循环 循环是很常用的一个控制结构,大部分东西要依靠它来完成,在JavaScript中,我们可以使用for(;;),while(),for(in)三种循环,事实上,这三种循环中for(in)的效率极差, ...

  2. createwindow

    WNDCLASS wndclass; wndclass.hbrBackground=(HBRUSH)getstockobject(WHITE_BRUSH); wndclass.hCursor=Load ...

  3. seo初学

    对前端而言,做网站采用扁平式结构:控制网页链接数量,不能太少,当然也不能太多:其次采用扁平的目录层次,不能超过3次:三:导航优化,最好是文字,如果是图片的话,alt和title必须添加. 面包屑导航: ...

  4. Css溢出隐藏

    display: -webkit-box;-webkit-line-clamp: 2;     /*多少行数之后显示为省略...*/word-wrap: break-word;word-break: ...

  5. 一个简单的javascript获取URL参数的代码

    function request(paras){ var url = location.href; var paraString = url.substring(url.indexOf("? ...

  6. Asp.net core与golang web简单对比测试

    最近因为工作需要接触了go语言,又恰好asp.net core发布RC2,就想简单做个对比测试. 下面是测试环境: CPU:E3-1230 v2 内存:16G 电脑有点不给力 操作系统:Centos7 ...

  7. Docker for Windows

    Docker for Windows使用简介 在上一篇文章中,通过演练指导的方式,介绍了在Docker中运行ASP.NET Core Web API应用程序的过程.本文将介绍Docker for Wi ...

  8. -fembed-bitcode is not supported on versions of iOS prior to 6.0

    -fembed-bitcode is not supported on versions of iOS prior to 6.0   说法二 错误提示 -fembed-bitcode is not s ...

  9. ssh公钥自动登陆

    第一步,在服务器上安装ssh服务 sudo apt-get install ssh 通过ssh -v查看是否安装成功 第二步创建本地公钥秘钥对 ssh-keygen -t rsa  //创建ssh公钥 ...

  10. C# and android

    http://www.cnblogs.com/GoodHelper/archive/2011/07/08/android_socket_chart.html http://blog.csdn.net/ ...