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 ...
随机推荐
- paip.按键替换映射总结
paip.按键替换映射总结 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/attilax 因为 ...
- github上的QT源码,必要的时候还是应该看一下,仅凭猜测很容易出错
QCoreApplication::processEvents 他处理的时候拿的是current不是qAppqApp的话,才是和主线程密切相关的 一直觉得QT源码复杂,有点怕,所以没怎么看 我也看不懂 ...
- int 转换成 CString(VC2008里有这个问题)
int s = 123; CString str; str.Format("%d",s); 这样就可以了,但是有的会提示这个错误 如果出现这个错误,就改成下面这个就OK了: st ...
- 基于visual Studio2013解决面试题之1207堆排序
题目
- 京JS 2013 - A two-day conference in Beijing for the JavaScript and Node.js community
京JS 2013 - A two-day conference in Beijing for the JavaScript and Node.js community 关于技术大会 京JS 2013 ...
- POJ 1042 Gone Fishing (贪心)(刘汝佳黑书)
Gone Fishing Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 30281 Accepted: 9124 Des ...
- [置顶] Windows Phone后台音乐详解一
应用于: Windows Phone 8 | Windows PhoneOS 7.1 你可以为winphone编写在后台播放音乐的app.这表示即使当用户点击返回或开始按钮离开你的应用界面时,你的应用 ...
- 08-使用for循环输出杨辉三角(循环)
/** * 使用循环输出杨辉三角 * * */ public class Test6 { public static void main(String[] args) { // 创建二维数组 int ...
- 程序实现LayoutAnimationController
在res/anim下新建anim_set.xml: <?xml version="1.0" encoding="utf-8"?> <set x ...
- Mac上利用Eclipse编译Cocos2d-x
目前使用较多的Cocos2d-x开发平台是XCode,应该是由于大部分Cocos2d-x开发者都是iOS开发出生.但是当我们将XCode开发的Cocos2d-x工程发布Android版本时,每次都需要 ...