UITableView性能优化及手工绘制UITableViewCell
提高表视图的性能
手工绘制单元格
- (void)loadAnimals
{
_animalList = [NSMutableArray arrayWithCapacity:rAnimalCount];
for (NSInteger i = 0; i < rAnimalCount; i++)
{
Animal *animal = [[Animal alloc] init];
NSString *name = [NSString stringWithFormat:@"Animal-%03d", i+1];
NSString *detail = [NSString stringWithFormat:@"dog or cat?"];
NSInteger seed = arc4random()%8 + 1;
NSString *imageName = [NSString stringWithFormat:@"head%02d", seed+1]; animal.name = name;
animal.detail = detail;
animal.imageName = imageName; [_animalList addObject:animal];
}
}
重点在于绘制,我首先创建了一个继承自UITableViewCell的父类一会让我们的单元格继承它,父类中有一个UIView类型的contentView成员,所有的绘制将在这个成员上进行。
@interface HRCellView : UIView
@end
@implementation HRCellView
- (void)drawRect:(CGRect)rect
{
[(HRCell *)[self superview] drawContentView:rect];
}
@end @implementation HRCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
contentView = [[HRCellView alloc] init];
contentView.opaque = YES; //不透明,提升渲染性能
[self addSubview:contentView];
}
return self;
} - (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
CGRect b = [self bounds];
b.size.height -= 1; // 给分割线留出位置
contentView.frame = b;
} - (void)setNeedsDisplay
{
[super setNeedsDisplay];
[contentView setNeedsDisplay];
} - (void)drawContentView:(CGRect)rect
{
//子类实现
}
@end
static UIFont *NameFont;
static UIFont *DetailFont; @implementation HRCustomCell + (void)initialize
{
NameFont = [UIFont fontWithName:@"American Typewriter" size:rNameFontSize];
DetailFont = [UIFont fontWithName:@"American Typewriter" size:rDetailFontSize];
}
将数据绑定到这个单元格中
- (void)bindAnimal:(Animal *)animal
{
if (_nameText != animal.name)
{
_nameText = animal.name;
}
if (_detailText != animal.detail)
{
_detailText = animal.detail;
}
if (_imageName != animal.imageName)
{
_imageName = animal.imageName;
} [self setNeedsDisplay];
}
- (void)drawContentView:(CGRect)rect
{
static UIColor *nameColor;
nameColor = [UIColor blackColor];
static UIColor *detailColor;
detailColor = [UIColor darkGrayColor]; CGContextRef context = UIGraphicsGetCurrentContext();
CGRect cellRect = self.frame; if (self.highlighted || self.selected) //选择或高亮时对应的颜色
{
CGContextSetFillColorWithColor(context, [UIColor lightGrayColor].CGColor);
CGContextFillRect(context, CGRectMake(0, 0, cellRect.size.width, cellRect.size.height));
}
else
{
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextFillRect(context, CGRectMake(0, 0, cellRect.size.width, cellRect.size.height));
} UIImage *image = [UIImage imageNamed:_imageName];
[image drawInRect:CGRectMake(5, 5, 50, 50)]; [nameColor set];
[_nameText drawAtPoint:CGPointMake(65, 10)
forWidth:200
withFont:NameFont
fontSize:rNameFontSize
lineBreakMode:NSLineBreakByWordWrapping
baselineAdjustment:UIBaselineAdjustmentAlignBaselines]; [detailColor set];
[_detailText drawAtPoint:CGPointMake(180, 40)
forWidth:120
withFont:DetailFont
fontSize:rDetailFontSize
lineBreakMode:NSLineBreakByWordWrapping
baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
}
虽然方法很长,但是只要会简单的quartz绘图这些都是最基础的方法。
UITableView性能优化及手工绘制UITableViewCell的更多相关文章
- uitableview性能优化(转)
这个感觉写的很好 收藏一下 以备后用 转自 http://www.cnblogs.com/pengyingh/articles/2354714.html 在iOS应用中,UITableView应该是使 ...
- 【原/转】UITableview性能优化总结
UITableView作为ios中使用最频繁的控件之一,其性能优化也是常常要面对的,尤其是当数据量偏大并且设备性能不足时.本文旨在总结tableview的几个性能优化tips,并且随着认识的深入,本文 ...
- UITableView性能优化
关于UITableView的性能优化,网络上也有一些总结.在这里就介绍下我们项目中遇到的问题以及对应的解决方法.相信我们遇到的问题也有一定的普适性,能够作为其他问题的优化方案. Instruments ...
- UITableView性能优化【本文摘自智车芯官网】
UITableView是个表格视图,可以在表格行空间中添加多个子控件,UITableView继承了UIScrollView,默认状态下可以堆单元格进行滚动,所有的UITableViewControll ...
- Android UI性能优化实战 识别绘制中的性能问题
转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/45556391: 本文出自:[张鸿洋的博客] 1.概述 2015年初google ...
- android性能优化练习:过度绘制
练习:https://github.com/zhangbz/AndroidUIPorblems 查看过度绘制 在开发者选项中开启"调试GPU过度绘制" 判断标准 无色:没有过度绘制 ...
- iOS边练边学--UITableView性能优化之三种方式循环利用
一.cell的循环利用方式1: /** * 什么时候调用:每当有一个cell进入视野范围内就会调用 */ - (UITableViewCell *)tableView:(UITableView *)t ...
- UITableView 性能优化
网络图片异步加载,SDWebImage. 文字直接 drawInRect/drawAtPoint 绘制,参考 ABTableViewCell,AdvancedTableViewCells. 本地图片也 ...
- 【解决方法】UITableView 性能优化笔记
1.网络图片异步载入,SDWebImage. 2.文字直接 drawInRect/drawAtPoint 绘制,參考 ABTableViewCell.AdvancedTableViewCells. 3 ...
随机推荐
- Android 设备管理器 阻止用户取消激活
该方案测试可行,系统版本4.4.2.它算是借助android系统的一个bug,不确定在后续更高的版本中是否修复. 该功能和360防卸载功能一样的实现原理. 主要的参考资料是:http://bbs.pe ...
- 虚拟化:搭建本地虚拟化环境和安装ubuntu操作系统
本文介绍如何在本地(windows操作系统)安装虚拟机,并在虚拟机下安装ubuntu操作系统. 一.机器升级 因为是在我的笔记本电脑上操作.首先升级了我的笔记本,买了内存条,将我机器的内存增加到8G, ...
- Python多线程下的_strptime问题
Python多线程下的_strptime问题 由于Python的datetime和time中的_strptime方法不支持多线程,运行时会报错: import datetimeimport threa ...
- 有关oppo蝴蝶解锁的三D技术
oppo手机的界面设计也是很漂亮的.在很多界面中使用了3D技术塑造出了大量华丽的效果.在蝴蝶解锁中使用了两个对称的三D变幻,宛如蝴蝶翅膀上美丽的花纹.在受到用户点击后,随风缓慢上下扇动,充满浪漫的动感 ...
- Project configuration is not up-to-date with pom.xml错误解决方法
导入一个Maven项目之后发现有一个如下的错误: Project configuration is not up-to-date with pom.xml. Run project configura ...
- Javascript 中的非空判断 undefined,null, NaN的区别
JS 数据类型 在介绍这三个之间的差别之前, 先来看一下JS 的数据类型. 在 Java ,C这样的语言中, 使用一个变量之前,需要先定义这个变量并指定它的数据类型,是整型,字符串型,.... 但是 ...
- java绘图板
JAVA绘图板 import java.awt.BasicStroke; import java.awt.BorderLayout; import java.awt.Button; import ja ...
- UVA 10574 - Counting Rectangles(枚举+计数)
10574 - Counting Rectangles 题目链接 题意:给定一些点,求可以成几个边平行于坐标轴的矩形 思路:先把点按x排序,再按y排序.然后用O(n^2)的方法找出每条垂直x轴的边,保 ...
- asp.net2.0安全性(2)--用户个性化设置(2)--转载来自车老师
上一篇我们用Profile.age等方式可以读取用户的年龄和其它的信息,但有的时候我们要查询显示所有用户的信息,但asp.net没有提供查询所有用户信息的功能,我们只能对现有的用户逐一查询其Profi ...
- 【linux】UBUNTU 12.04下傻瓜式简单安装arm-linux-gcc等gnu arm toolchain交叉编译工具
欢迎转载,转载时请保留作者信息,谢谢. 邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http:// ...