对于一个标准的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)];
}
注:要把CustomDrawingView的背景颜色设置为白色。默认背景颜色是黑色。
1)cell高度的计算速度要足够快;
2)透明度尽量保持为1.0;
3)避免使用图形特效;