UITableView 是 iOS 開發(fā)中的常用控件,用來(lái)加載列表數(shù)據(jù),當(dāng)列表數(shù)據(jù)量大或者列表布局過(guò)于復(fù)雜的時(shí)候有可能出現(xiàn)卡頓,影響用戶體驗(yàn),這個(gè)時(shí)候就要考慮對(duì) UITableView 進(jìn)行優(yōu)化了。在這里將學(xué)習(xí) VVebo 的 UITableView 優(yōu)化技巧的心得記錄下來(lái)。
緩存 Cell 高度
UITableView 的 UITableViewDelegate 里面有個(gè)方法 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 專門用于獲取 Cell 的高度 。由于UITableView在繪制 Cell 的時(shí)候每次會(huì)主動(dòng)獲取 Cell 的高度,所以這里的優(yōu)化點(diǎn)是減少該方法的執(zhí)行時(shí)間。保存第一次計(jì)算出來(lái)的 Cell 高度,并保存到 Cell 對(duì)應(yīng)的 Model 上 ,而不是每次重復(fù)計(jì)算 Cell 的高度,可以達(dá)到減少該方法的執(zhí)行時(shí)間的目的。
//獲取數(shù)據(jù),并計(jì)算 frame
- (void)loadData{
NSArray *temp = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]];
// 省略代碼
for (NSDictionary *dict in temp) {
NSDictionary *retweet = [dict valueForKey:@"retweeted_status"];
if (retweet) {
// ... 省略代碼
{
// 計(jì)算 View 的 frame
float width = [UIScreen screenWidth]-SIZE_GAP_LEFT*2;
CGSize size = [subData[@"text"] sizeWithConstrainedToWidth:width fromFont:FontWithSize(SIZE_FONT_SUBCONTENT) lineSpace:5];
NSInteger sizeHeight = (size.height+.5);
subData[@"textRect"] = [NSValue valueWithCGRect:CGRectMake(SIZE_GAP_LEFT, SIZE_GAP_BIG, width, sizeHeight)];
sizeHeight += SIZE_GAP_BIG;
if (subData[@"pic_urls"] && [subData[@"pic_urls"] count]>0) {
sizeHeight += (SIZE_GAP_IMG+SIZE_IMAGE+SIZE_GAP_IMG);
}
sizeHeight += SIZE_GAP_BIG;
// 保存 View 的 frame
subData[@"frame"] = [NSValue valueWithCGRect:CGRectMake(0, 0, [UIScreen screenWidth], sizeHeight)];
}
data[@"subData"] = subData;
}
// ... 省略代碼
{
// ... 省略代碼
NSMutableDictionary *subData = [data valueForKey:@"subData"];
if (subData) {
// 計(jì)算 View 的 frame
sizeHeight += SIZE_GAP_BIG;
CGRect frame = [subData[@"frame"] CGRectValue];
CGRect textRect = [subData[@"textRect"] CGRectValue];
frame.origin.y = sizeHeight;
subData[@"frame"] = [NSValue valueWithCGRect:frame];
textRect.origin.y = frame.origin.y+SIZE_GAP_BIG;
subData[@"textRect"] = [NSValue valueWithCGRect:textRect];
sizeHeight += frame.size.height;
data[@"subData"] = subData;
}
sizeHeight += 30;
// 保存 View 的 frame
data[@"frame"] = [NSValue valueWithCGRect:CGRectMake(0, 0, [UIScreen screenWidth], sizeHeight)];
}
[datas addObject:data];
}
}
計(jì)算出來(lái)的 Cell 高度可以在 UITableViewDelegate 使用。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *dict = datas[indexPath.row];
float height = [dict[@"frame"] CGRectValue].size.height;
return height;
}
異步渲染內(nèi)容到圖片

這是一張典型的微博頁(yè)面,這個(gè)頁(yè)面若是采用普通的 View 控件拼湊的話,會(huì)需要多少個(gè)控件呢?我們這里簡(jiǎn)單劃分一下。粗略劃分了一下,我們大概需要 13 個(gè) view 才能完成。

對(duì) TableView 的優(yōu)化有時(shí)候可以直接考慮對(duì) TableViewCell 的優(yōu)化。對(duì)于復(fù)雜 View 的優(yōu)化,首先考慮減少 View 的布局層級(jí)。我們將這個(gè)復(fù)雜的問(wèn)題簡(jiǎn)單化,我們把 TableViewCell 按下圖所示分割成三個(gè)部分,分別用紅色,綠色,藍(lán)色區(qū)分開來(lái)。 通過(guò)和實(shí)際的頁(yè)面對(duì)比,我們可以看到紅色部分的名字,日期,來(lái)源以及藍(lán)色部分相對(duì)來(lái)說(shuō)比較簡(jiǎn)單,布局變化比較小,所以我們可以考慮將這些內(nèi)容全部繪制到一張圖片上,來(lái)達(dá)到減少 View 的布局層級(jí)的目的。

我們排除其他干擾控件,使用 Xcode 來(lái)查看 TableViewCell 的布局層次,可以清晰的看到紅色部分的名字,日期,來(lái)源以及整個(gè)藍(lán)色藍(lán)色部分都是直接繪制在圖片上,圖片使用一個(gè) UIImageView 來(lái)承載。

// 使用 CoreText 繪制
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
CGRect rect = [_data[@"frame"] CGRectValue];
UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
// 最外層的大框
// [[UIColor colorWithRed:250/255.0 green:250/255.0 blue:250/255.0 alpha:1] set];
[[UIColor redColor] set];
CGContextFillRect(context, rect);
if ([_data valueForKey:@"subData"]) {
// 第二層框
// [[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1] set];
[[UIColor greenColor] set];
CGRect subFrame = [_data[@"subData"][@"frame"] CGRectValue];
CGContextFillRect(context, subFrame);
// 線
[[UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:1] set];
CGContextFillRect(context, CGRectMake(0, subFrame.origin.y, rect.size.width, 0.5));
}
{
// 名字
float leftX = SIZE_GAP_LEFT+SIZE_AVATAR+SIZE_GAP_BIG;
float x = leftX;
float y = (SIZE_AVATAR-(SIZE_FONT_NAME+SIZE_FONT_SUBTITLE+6))/2-2+SIZE_GAP_TOP+SIZE_GAP_SMALL-5;
[_data[@"name"] drawInContext:context withPosition:CGPointMake(x, y) andFont:FontWithSize(SIZE_FONT_NAME)
andTextColor:[UIColor colorWithRed:106/255.0 green:140/255.0 blue:181/255.0 alpha:1]
andHeight:rect.size.height];
y += SIZE_FONT_NAME+5;
float fromX = leftX;
float size = [UIScreen screenWidth]-leftX;
// 時(shí)間和來(lái)源
NSString *from = [NSString stringWithFormat:@"%@ %@", _data[@"time"], _data[@"from"]];
[from drawInContext:context withPosition:CGPointMake(fromX, y) andFont:FontWithSize(SIZE_FONT_SUBTITLE)
andTextColor:[UIColor colorWithRed:178/255.0 green:178/255.0 blue:178/255.0 alpha:1]
andHeight:rect.size.height andWidth:size];
}
{
CGRect countRect = CGRectMake(0, rect.size.height-30, [UIScreen screenWidth], 30);
// [[UIColor colorWithRed:250/255.0 green:250/255.0 blue:250/255.0 alpha:1] set];
[[UIColor blueColor] set];
CGContextFillRect(context, countRect);
float alpha = 1;
// 評(píng)論
float x = [UIScreen screenWidth]-SIZE_GAP_LEFT-10;
NSString *comments = _data[@"comments"];
if (comments) {
CGSize size = [comments sizeWithConstrainedToSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) fromFont:FontWithSize(SIZE_FONT_SUBTITLE) lineSpace:5];
x -= size.width;
[comments drawInContext:context withPosition:CGPointMake(x, 8+countRect.origin.y)
andFont:FontWithSize(12)
andTextColor:[UIColor colorWithRed:178/255.0 green:178/255.0 blue:178/255.0 alpha:1]
andHeight:rect.size.height];
// 圖片畫到 context
[[UIImage imageNamed:@"t_comments.png"] drawInRect:CGRectMake(x-5, 10.5+countRect.origin.y, 10, 9) blendMode:kCGBlendModeNormal alpha:alpha];
commentsRect = CGRectMake(x-5, self.height-50, [UIScreen screenWidth]-x+5, 50);
x -= 20;
}
// 轉(zhuǎn)發(fā)
NSString *reposts = _data[@"reposts"];
if (reposts) {
CGSize size = [reposts sizeWithConstrainedToSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) fromFont:FontWithSize(SIZE_FONT_SUBTITLE) lineSpace:5];
x -= MAX(size.width, 5)+SIZE_GAP_BIG;
[reposts drawInContext:context withPosition:CGPointMake(x, 8+countRect.origin.y)
andFont:FontWithSize(12)
andTextColor:[UIColor colorWithRed:178/255.0 green:178/255.0 blue:178/255.0 alpha:1]
andHeight:rect.size.height];
[[UIImage imageNamed:@"t_repost.png"] drawInRect:CGRectMake(x-5, 11+countRect.origin.y, 10, 9) blendMode:kCGBlendModeNormal alpha:alpha];
repostsRect = CGRectMake(x-5, self.height-50, commentsRect.origin.x-x, 50);
x -= 20;
}
// ...
[@"???" drawInContext:context
withPosition:CGPointMake(SIZE_GAP_LEFT, 8+countRect.origin.y)
andFont:FontWithSize(11)
andTextColor:[UIColor colorWithRed:178/255.0 green:178/255.0 blue:178/255.0 alpha:.5]
andHeight:rect.size.height];
if ([_data valueForKey:@"subData"]) {
// 線
[[UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:1] set];
CGContextFillRect(context, CGRectMake(0, rect.size.height-30.5, rect.size.width, .5));
}
}
// 獲取繪制的圖片,然后切換到主線程設(shè)置圖片
UIImage *temp = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
dispatch_async(dispatch_get_main_queue(), ^{
if (flag==drawColorFlag) {
postBGView.frame = rect;
postBGView.image = nil;
postBGView.image = temp;
}
});
});
在上面代碼中,除了做到減少 View 的布局層級(jí)之外還使用了一個(gè)非常重要技術(shù)-異步渲染內(nèi)容到圖片。使用 dispatch_async 將繪制工作放到后臺(tái)操作,減少主線程的計(jì)算工作量。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{});
使用 CGContextFillRect 用于填充 View 的背景顏色。
// [[UIColor colorWithRed:250/255.0 green:250/255.0 blue:250/255.0 alpha:1] set];
[[UIColor redColor] set];
CGContextFillRect(context, rect);
利用 CoreText 來(lái)做文本排版,具體的 CoreText 實(shí)現(xiàn)細(xì)節(jié)可以參考 Demo 代碼
// 名字
float leftX = SIZE_GAP_LEFT+SIZE_AVATAR+SIZE_GAP_BIG;
float x = leftX;
float y = (SIZE_AVATAR-(SIZE_FONT_NAME+SIZE_FONT_SUBTITLE+6))/2-2+SIZE_GAP_TOP+SIZE_GAP_SMALL-5;
[_data[@"name"] drawInContext:context withPosition:CGPointMake(x, y) andFont:FontWithSize(SIZE_FONT_NAME)
andTextColor:[UIColor colorWithRed:106/255.0 green:140/255.0 blue:181/255.0 alpha:1]
andHeight:rect.size.height];
y += SIZE_FONT_NAME+5;
float fromX = leftX;
float size = [UIScreen screenWidth]-leftX;
// 時(shí)間和來(lái)源
NSString *from = [NSString stringWithFormat:@"%@ %@", _data[@"time"], _data[@"from"]];
[from drawInContext:context withPosition:CGPointMake(fromX, y) andFont:FontWithSize(SIZE_FONT_SUBTITLE)
andTextColor:[UIColor colorWithRed:178/255.0 green:178/255.0 blue:178/255.0 alpha:1]
andHeight:rect.size.height andWidth:size];
異步生成圖片之后切換到主線程設(shè)置圖片
dispatch_async(dispatch_get_main_queue(), ^{
if (flag==drawColorFlag) {
postBGView.frame = rect;
postBGView.image = nil;
postBGView.image = temp;
}
});
處理好了減少 View 布局層級(jí)和異步繪制之后,我們還需要處理一個(gè)圓角頭像的問(wèn)題。圓角頭像最簡(jiǎn)單的處理方法就是使用一張圓形鏤空的圖片來(lái)實(shí)現(xiàn),不過(guò)這個(gè)實(shí)現(xiàn)方案有個(gè)缺陷就是對(duì) View 的背景顏色有要求。這里采用的處理方案就是這個(gè)最簡(jiǎn)單的處理方法。

處理好了背景問(wèn)題,接下來(lái)時(shí)候看看微博正文的問(wèn)題了。微博的正文放在 label 控件里面,而轉(zhuǎn)發(fā)的微博詳情內(nèi)容放在 detailLabel 里面。這個(gè) label 是自定義控件 VVeboLabel,里面的 - (void)setText:(NSString *)text 方法具體的實(shí)現(xiàn)方式也是采用 CoreText 異步繪制實(shí)現(xiàn)的。
//設(shè)置文本內(nèi)容,將文本內(nèi)容設(shè)置在單獨(dú)的 View 上面
- (void)drawText{
if (label==nil||detailLabel==nil) {
[self addLabel];
}
label.frame = [_data[@"textRect"] CGRectValue];
[label setText:_data[@"text"]];
if ([_data valueForKey:@"subData"]) {
detailLabel.frame = [[_data valueForKey:@"subData"][@"textRect"] CGRectValue];
[detailLabel setText:[_data valueForKey:@"subData"][@"text"]];
detailLabel.hidden = NO;
}
}

可能你會(huì)問(wèn)了,使用 CoreText 異步繪制的文本內(nèi)容如何設(shè)置監(jiān)聽事件呢?CoreText 又如何處理點(diǎn)擊高亮問(wèn)題呢?我們通過(guò) - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 方法來(lái)獲取用戶的點(diǎn)擊位置。將獲取到的用戶點(diǎn)擊位置與事先保存文本位置比較,若是用戶點(diǎn)擊位置位于文本區(qū)域內(nèi),那么說(shuō)明用戶點(diǎn)擊了文本。為了能夠做出高亮效果,VVeboLabel 控件內(nèi)部必須維護(hù)一個(gè)字段 highlighting 和一個(gè)用于顯示高亮文本圖片的 highlightImageView,當(dāng) highlighting == YES 的時(shí)候,異步繪制高亮文本內(nèi)容生成圖片并使用 highlightImageView 顯示該圖片,用于表示控件的高亮狀態(tài)。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint location = [[touches anyObject] locationInView:self];
for (NSString *key in framesDict.allKeys) {
CGRect frame = [[framesDict valueForKey:key] CGRectValue];
// 將獲取到的用戶點(diǎn)擊位置與事先保存文本位置比較
if (CGRectContainsPoint(frame, location)) {
NSRange range = NSRangeFromString(key);
range = NSMakeRange(range.location, range.length-1);
currentRange = range;
[self highlightWord];
// *** 省略代碼
}
}
}
// VVeboLabel.m
// ... 省略代碼
if (isHighlight) {
if (highlighting) {
highlightImageView.image = nil;
if (highlightImageView.width!=screenShotimage.size.width) {
highlightImageView.width = screenShotimage.size.width;
}
if (highlightImageView.height!=screenShotimage.size.height) {
highlightImageView.height = screenShotimage.size.height;
}
highlightImageView.image = screenShotimage;
}
} else {
if ([temp isEqualToString:text]) {
if (labelImageView.width!=screenShotimage.size.width) {
labelImageView.width = screenShotimage.size.width;
}
if (labelImageView.height!=screenShotimage.size.height) {
labelImageView.height = screenShotimage.size.height;
}
highlightImageView.image = nil;
labelImageView.image = nil;
labelImageView.image = screenShotimage;
}
}
// ... 省略代碼
VVeboLabel 控件處理高亮情況的 View 結(jié)構(gòu)層次如下圖。

按需加載內(nèi)容
UITableView 的優(yōu)化除了在 UITableViewCell 的繪制方面優(yōu)化之后,還可以在加載數(shù)據(jù)方面優(yōu)化,按需加載內(nèi)容,避免加載暫時(shí)無(wú)用的數(shù)據(jù),從而減少數(shù)據(jù)量,減少 UITableView 的繪制工作量,達(dá)到優(yōu)化的目的。
判斷按需加載的 indexPaths , 如果目標(biāo)行與當(dāng)前行相差超過(guò)指定行數(shù),只在目標(biāo)滾動(dòng)范圍的前后指定3行加載。這樣可以減少 UITableView 的繪制工作量
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
NSIndexPath *ip = [self indexPathForRowAtPoint:CGPointMake(0, targetContentOffset->y)];
NSIndexPath *cip = [[self indexPathsForVisibleRows] firstObject];
NSInteger skipCount = 8;
// 目標(biāo)行與當(dāng)前行相差超過(guò)指定行數(shù)
if (labs(cip.row-ip.row)>skipCount) {
// 目標(biāo)位置的行
NSArray *temp = [self indexPathsForRowsInRect:CGRectMake(0, targetContentOffset->y, self.width, self.height)];
NSMutableArray *arr = [NSMutableArray arrayWithArray:temp];
// velocity.y<0 下拉, velocity.y>0 上拉
if (velocity.y<0) {
NSIndexPath *indexPath = [temp lastObject];
if (indexPath.row+3<datas.count) {
[arr addObject:[NSIndexPath indexPathForRow:indexPath.row+1 inSection:0]];
[arr addObject:[NSIndexPath indexPathForRow:indexPath.row+2 inSection:0]];
[arr addObject:[NSIndexPath indexPathForRow:indexPath.row+3 inSection:0]];
}
} else {
NSIndexPath *indexPath = [temp firstObject];
if (indexPath.row>3) {
[arr addObject:[NSIndexPath indexPathForRow:indexPath.row-3 inSection:0]];
[arr addObject:[NSIndexPath indexPathForRow:indexPath.row-2 inSection:0]];
[arr addObject:[NSIndexPath indexPathForRow:indexPath.row-1 inSection:0]];
}
}
[needLoadArr addObjectsFromArray:arr];
}
}
當(dāng) UITableView 開始繪制 Cell 的時(shí)候,若是 indexpath 包含在按需繪制的 needLoadArr 數(shù)組里面,那么就異步繪制該 Cell ,如果沒(méi)有則跳過(guò)該 Cell 。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
VVeboTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell==nil) {
cell = [[VVeboTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"cell"];
}
// 繪制 Cell
[self drawCell:cell withIndexPath:indexPath];
return cell;
}
// 按需繪制 Cell
- (void)drawCell:(VVeboTableViewCell *)cell withIndexPath:(NSIndexPath *)indexPath{
NSDictionary *data = [datas objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell clear];
cell.data = data;
// 按需繪制,只要在 needLoadArr 里面的 indexPath 才需要繪制 Cell
if (needLoadArr.count>0&&[needLoadArr indexOfObject:indexPath]==NSNotFound) {
[cell clear];
return;
}
if (scrollToToping) {
return;
}
[cell draw];
}
總結(jié)
主要介紹了 3 個(gè)優(yōu)化技巧
- 緩存 Cell 高度
- 異步渲染內(nèi)容到圖片
- 按照滑動(dòng)速度按需加載內(nèi)容
這篇博客文章主要是學(xué)習(xí) VVebo 的 UITableView 優(yōu)化技巧,VVebo 的作者將 VVeboTableViewDemo 開源在 GitHub,大家可以查閱代碼,感謝作者。這個(gè)方案也有存在一個(gè)不足,在 TableView 快速滑動(dòng)的時(shí)候,頁(yè)面會(huì)出現(xiàn)空白。
參考
- http://blog.devtang.com/2015/06/27/using-coretext-1/
- http://blog.devtang.com/2015/06/27/using-coretext-2/
- http://blog.devtang.com/2014/01/23/the-issue-of-non-breaking-space-in-coretext/
- http://beyondvincent.com/2013/11/12/2013-11-12-121-brief-analysis-text-kit/#1
- https://github.com/johnil/VVeboTableViewDemo