UItableView优化

对于一个标准的UITableViewCell来讲,通常的渲染速度是55-60FPS;

优化方法:

1.复用UITableViewCell;

只需要按标准的UITableViewCell调用重用机制;


2.将网络请求的图像数据 先做一次缓存在放在Cell中;

imageNamed 做了一个很重要的工作,它将所加载的图像在内存中进行了缓存,当再次调用这个图像,就能直接从内存进行复用,但是这个方法的问题是它只能从你的Bundle中加载图像,也就是只能加载源码中的图像。

我们通常用initWithContentsOfFile或者initWithData这两个方法从网络加载图像,使用这两个方法的时候,是不会进行自动进行缓存操作的。因此我们可以在内存中创建一个Dictionary来缓存网络数据;另一个处理文件加载问题的途径就是使用多线程;

简单例子:

- (UIImage *)imageWithName:(NSString *)name
 {
if ([self.imageDictionary objectForKey:name])
{
return [self.imageDictionary objectForKey:name];
}

UIImage *image = [[UIImage alloc] initWithContentsOfFile:name]; 
[self.imageDictionary setObject:imageforKey:name];
return image;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
  ReuseTableViewCell *cell = (ReuseTableViewCell *) [self getCellWithTableView:tableView cellIdentifier:CellIdentifier nibName:@"ReuseTableViewCell"];
NSString *avatarFile = [NSString stringWithFormat:@"a0"];
NSString *avatarName = [[NSBundle mainBundle] pathForResource:avatarFile ofType:@"jpeg"];
cell.avatar.image = [self imageWithName:avatarName];
cell.userName.text = [NSString stringWithFormat:@"hi here: %d", indexPath.row];
 return cell;


3.当一个Cell的子视图过多的时候,可以考虑重绘这个Cell;

创建一个自定义UITabelViewCell类,

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier {

if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]) {

CGRect subFrame = CGRectMake(0.0, 0.0,self.contentView.bounds.size.width, self.contentView.bounds.size.height);

drawingView = [[CustomDrawingView alloc] initWithFrame: subFrame];

drawingView.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;

[self.contentView addSubview:drawingView];

}

return self;

}

在 CustomDrawingView.m
- (void)drawRect:(CGRect)rect {

self.backgroundColor = [UIColor whiteColor];
// Drawing code.
[self.userName drawInRect:CGRectMake(70,0, 95, 21) withFont:userNameFont 
lineBreakMode:UILineBreakModeTailTruncation alignment:UIBaselineAdjustmentAlignBaselines];

// Drawing Image
[self.avatarImage drawInRect:CGRectMake(20, 5, 36, 34)];

// Drawing button

[self.button drawInRect:CGRectMake(50, 5, 36, 34)];

自定义的绘制代码之所以比加载nib文件或直接添加子视图这两种方法性能更优越的原因就在于GPU(图像处理器)将会负责自定义的绘制代码。GPU渲染和显示UI的速度极快;因此,自定义的绘制代码只绘制具有复杂子视图结构的最佳方法。

注:要把CustomDrawingView的背景颜色设置为白色。默认背景颜色是黑色。


4.其他的相关技术

1)cell高度的计算速度要足够快;

2)透明度尽量保持为1.0;

3)避免使用图形特效;



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值