Cell是我们经常会使用的控件,以下几种方法可以大大提高性能
不透明的视图可以极大地提高渲染的速度 将UITableViewCell及其子视图的opaque属性设为YES(默认值) 背景色的alpha值应该为1(例如不要使用clearColor) 图像的alpha值也应该为1,或者在画图时设为不透明
UITableView只需要一屏幕的UITableViewCell对象即可。因此在UITableViewCell不可见时将其缓存起来,而在需要时继续使用它即可 而UITableView也提供了这种机制,只需要简单地设置一个identifier即可:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"xxx"]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"xxx"]; }值得一提UITableViewCell被重用时它内部绘制的内容并不会被自动清除,因此你需要调用setNeedsDisplayInRect:或setNeedsDisplay 在添加UITableViewCell的时候如果不需要动画效果,最好不要使用insertRowsAtIndexPaths:withRowAnimation:方法,而是直接调用reloadData方法来刷新
UITableViewCell包含了textLabel、detailTextLabel和imageView等,而你还可以自定义视图放在它的contentView里。然而view是很大的对象,创建它会消耗较多资源并且也影响渲染的性能。 如果你的UITableViewCell包含图片且数目较多,使用默认的UITableViewCell会非常影响性能,使用自定义的view明显会快些。 最佳的解决办法还是继承UITableViewCell,并在其drawRect:中自行绘制:
- (void)drawRect:(CGRect)rect { if (image) { [image drawAtPoint:imagePoint]; self.image = nil; }else { [placeHolder drawAtPoint:imagePoint]; } [text drawInRect:textRect withFont:font lineBreakMode:UILineBreakModeTailTruncation]; }选中这行UITableViewCell会变蓝,内容也被挡住。最简单的方法就是将取消高亮,UITableViewCell的selectionStyle属性设为UITableViewCellSelectionStyleNone。 此外还可以创建CALayer,将内容绘制到layer上,然后对UITableViewCell的contentView.layer调用addSublayer:方法。 如果layer透明、圆角、变形等效果,就会影响到绘制速度,解决办法可参见后面的预渲染图像。
CALayer * imageLayer = [CALayer layer]; imageLayer.bounds = CGRectMake(0,0,200,100); imageLayer.position = CGPointMake(200,200); imageLayer.contents = (id)[UIImage imageNamed:@"xx.jpg"].CGImage; imageLayer.contentsGravity = kCAGravityResizeAspect; [tableCell.layer addSublayer:imageLayer];在实现drawRect:的时候,它的rect参数就是需要绘制的区域,这个区域之外的不需要进行绘制。 例如上例中,就可以用CGRectIntersectsRect、CGRectIntersection或CGRectContainsRect判断是否需要绘制image和text,然后再调用绘制方法。
你会发现即使做到了上述几点,当新的图像出现时,仍然会有短暂的停顿现象。解决的办法就是在bitmap context里先将其画一遍,导出成UIImage对象,然后再绘制到屏幕,详细做法可见《利用预渲染加速iOS设备的图像显示》
做到前几点UITableView滚动时应该足够流畅了,不过仍可能让用户感到不爽。常见的现象就是在更新数据时,整个界面卡住不动完全不响应用户请求。出现这种现象的原因就是主线程执行了耗时很长的函数或方法,在其执行完毕前无法绘制屏幕和响应用户请求。其中最常见的就是网络请求,它通常都需要花费数秒的时间,而你不应该让用户等待那么久。 解决办法就是使用多线程,让子线程去执行这些函数或方法。这里面还有一个学问,当下载线程数超过2时,会显著影响主线程的性能。因此在使用ASIHTTPRequest时,可以用一个NSOperationQueue来维护下载请求,并将其maxConcurrentOperationCount设为2。而NSURLRequest则可以配合GCD来实现,或者使用NSURLConnection的setDelegateQueue:方法。当然,在不需要响应用户请求时,也可以增加下载线程数,以加快下载速度:
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { if (!decelerate) { queue.maxConcurrentOperationCount =5; } } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { queue.maxConcurrentOperationCount =5; } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { queue.maxConcurrentOperationCount =2; }此外,自动载入更新数据对用户来说也很友好,这减少了用户等待下载的时间。例如每次载入50条信息,那就可以在滚动到倒数第10条以内时,加载更多信息:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if (count - indexPath.row <10 && !updating) { updating = YES; [self update]; } }// update方法获取到结果后,设置updating为NO 还有一点要注意的就是当图片下载完成后,如果UITableViewCell是可见的,还需要更新图像
NSArray *indexPaths = [self.tableView indexPathsForVisibleRows]; for (NSIndexPath *visibleIndexPathin indexPaths) { if (indexPath == visibleIndexPath) { MyTableViewCell *cell = (MyTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath]; cell.image = image; [cell setNeedsDisplayInRect:imageRect]; break; } }// 也可不遍历,直接与头尾相比较,看是否在中间即可
最后还是前面所说过的insertRowsAtIndexPaths:withRowAnimation:方法,插入新行需要在主线程执行,而一次插入很多行的话,会长时间阻塞主线程。而换成reloadData方法的话,瞬间就处理完了