UITableView 是 iOS 開發(fā)中的常用控件澈蝙,用來加載列表數(shù)據(jù)美莫,當(dāng)列表數(shù)據(jù)量大或者列表布局過于復(fù)雜的時候有可能出現(xiàn)卡頓途凫,影響用戶體驗,這個時候就要考慮對 UITableView 進(jìn)行優(yōu)化了宗挥。在這里將學(xué)習(xí) VVebo 的 UITableView 優(yōu)化技巧的心得記錄下來。
緩存 Cell 高度
UITableView 的 UITableViewDelegate 里面有個方法 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 專門用于獲取 Cell 的高度 种蝶。由于UITableView在繪制 Cell 的時候每次會主動獲取 Cell 的高度契耿,所以這里的優(yōu)化點是減少該方法的執(zhí)行時間。保存第一次計算出來的 Cell 高度,并保存到 Cell 對應(yīng)的 Model 上 ,而不是每次重復(fù)計算 Cell 的高度螃征,可以達(dá)到減少該方法的執(zhí)行時間的目的搪桂。
//獲取數(shù)據(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) {
// ... 省略代碼
{
// 計算 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) {
// 計算 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];
}
}
計算出來的 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)容到圖片
這是一張典型的微博頁面酗电,這個頁面若是采用普通的 View 控件拼湊的話,會需要多少個控件呢内列?我們這里簡單劃分一下撵术。粗略劃分了一下,我們大概需要 13 個 view 才能完成话瞧。
對 TableView 的優(yōu)化有時候可以直接考慮對 TableViewCell 的優(yōu)化嫩与。對于復(fù)雜 View 的優(yōu)化,首先考慮減少 View 的布局層級交排。我們將這個復(fù)雜的問題簡單化划滋,我們把 TableViewCell 按下圖所示分割成三個部分,分別用紅色个粱,綠色古毛,藍(lán)色區(qū)分開來。 通過和實際的頁面對比都许,我們可以看到紅色部分的名字稻薇,日期,來源以及藍(lán)色部分相對來說比較簡單胶征,布局變化比較小塞椎,所以我們可以考慮將這些內(nèi)容全部繪制到一張圖片上,來達(dá)到減少 View 的布局層級的目的睛低。
我們排除其他干擾控件案狠,使用 Xcode 來查看 TableViewCell 的布局層次,可以清晰的看到紅色部分的名字钱雷,日期骂铁,來源以及整個藍(lán)色藍(lán)色部分都是直接繪制在圖片上,圖片使用一個 UIImageView 來承載罩抗。
// 使用 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;
// 時間和來源
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;
// 評論
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 的布局層級之外還使用了一個非常重要技術(shù)-異步渲染內(nèi)容到圖片恨憎。使用 dispatch_async 將繪制工作放到后臺操作吟榴,減少主線程的計算工作量。
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 來做文本排版烁挟,具體的 CoreText 實現(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;
// 時間和來源
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 布局層級和異步繪制之后,我們還需要處理一個圓角頭像的問題骨坑。圓角頭像最簡單的處理方法就是使用一張圓形鏤空的圖片來實現(xiàn)撼嗓,不過這個實現(xiàn)方案有個缺陷就是對 View 的背景顏色有要求。這里采用的處理方案就是這個最簡單的處理方法。
處理好了背景問題静稻,接下來時候看看微博正文的問題了警没。微博的正文放在 label 控件里面,而轉(zhuǎn)發(fā)的微博詳情內(nèi)容放在 detailLabel 里面振湾。這個 label 是自定義控件 VVeboLabel杀迹,里面的 - (void)setText:(NSString *)text 方法具體的實現(xiàn)方式也是采用 CoreText 異步繪制實現(xiàn)的。
//設(shè)置文本內(nèi)容押搪,將文本內(nèi)容設(shè)置在單獨的 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;
}
}
可能你會問了树酪,使用 CoreText 異步繪制的文本內(nèi)容如何設(shè)置監(jiān)聽事件呢?CoreText 又如何處理點擊高亮問題呢大州?我們通過 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 方法來獲取用戶的點擊位置续语。將獲取到的用戶點擊位置與事先保存文本位置比較,若是用戶點擊位置位于文本區(qū)域內(nèi)厦画,那么說明用戶點擊了文本疮茄。為了能夠做出高亮效果,VVeboLabel 控件內(nèi)部必須維護(hù)一個字段 highlighting 和一個用于顯示高亮文本圖片的 highlightImageView根暑,當(dāng) highlighting == YES 的時候力试,異步繪制高亮文本內(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];
// 將獲取到的用戶點擊位置與事先保存文本位置比較
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ù)據(jù)颇象,從而減少數(shù)據(jù)量伍伤,減少 UITableView 的繪制工作量,達(dá)到優(yōu)化的目的。
判斷按需加載的 indexPaths , 如果目標(biāo)行與當(dāng)前行相差超過指定行數(shù)遣钳,只在目標(biāo)滾動范圍的前后指定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)前行相差超過指定行數(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 的時候耍贾,若是 indexpath 包含在按需繪制的 needLoadArr 數(shù)組里面,那么就異步繪制該 Cell 路幸,如果沒有則跳過該 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 個優(yōu)化技巧
- 緩存 Cell 高度
- 異步渲染內(nèi)容到圖片
- 按照滑動速度按需加載內(nèi)容
這篇博客文章主要是學(xué)習(xí) VVebo 的 UITableView 優(yōu)化技巧简肴,VVebo 的作者將 VVeboTableViewDemo 開源在 GitHub晃听,大家可以查閱代碼,感謝作者。這個方案也有存在一個不足能扒,在 TableView 快速滑動的時候佣渴,頁面會出現(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