主 Bundle 欄
重點關注
**
表格的性能優(yōu)化:
1.行高一定要緩存 ;
2.不要動態(tài)創(chuàng)建子視圖 ;
<1>所有子視圖都預先創(chuàng)建 ;
<2>如果不需要顯示可以設置 hidden ;
3.所有子視圖都應該添加到 contentView 上 ;
因為 cell 有一個功能就是側滑可以調出刪除按鈕,或者添加選項等功能,如果我們的子視圖不在 contentView 上到時候要是有這個側滑的需求的話修改量很大!
4.所有子視圖都必須指定背景顏色 ;
<1>UIView 的背景顏色不設置的話 push 推出的時候會卡頓 ;
<2>NavigationController 的背景顏色如果不設置右上角會有黑條陰影,用戶體驗不好
5.所有顏色都不要使用透明度 alpha ;
如果設置了透明度圖層之間會有一個多層的渲染的過程,對性能的消耗非常大!尤其是 cell 里所有的顏色都不要使用 alpha,因為在快速滾動的時候,系統(tǒng)總想快速計算這個渲染的結果, 好讓 cell 顯示,這樣的話運算量過大
6.cell 柵格化 ;
7.異步繪制 ;
**
ViewController.m 文件
#import "ViewController.h"
//@import SDWebImage ;
#import "YYWebImage.h"
//YYWebImage 如果需要播放動圖,需要使用`YYAnimatedImageView`專門播放動圖用的!
#import "MDTableViewCell.h"
NSString *const cellIdentifier = @"cellIdentifier" ;
@interface ViewController () <UITableViewDataSource , UITableViewDelegate>
@end
/**
表格的性能優(yōu)化:
1.行高一定要緩存 ;
2.不要動態(tài)創(chuàng)建子視圖 ;
<1>所有子視圖都預先創(chuàng)建 ;
<2>如果不需要顯示可以設置 hidden ;
3.所有子視圖都應該添加到 contentView 上 ;
因為 cell 有一個功能就是側滑可以調出刪除按鈕,或者添加選項等功能,如果我們的子視圖不在 contentView 上到時候要是有這個側滑的需求的話修改量很大!
4.所有子視圖都必須指定背景顏色 ;
<1>UIView 的背景顏色不設置的話 push 推出的時候會卡頓 ;
<2>NavigationController 的背景顏色如果不設置右上角會有黑條陰影,用戶體驗不好
5.所有顏色都不要使用透明度 alpha ;
如果設置了透明度圖層之間會有一個多層的渲染的過程,對性能的消耗非常大!尤其是 cell 里所有的顏色都不要使用 alpha,因為在快速滾動的時候,系統(tǒng)總想快速計算這個渲染的結果, 好讓 cell 顯示,這樣的話運算量過大
6.cell 柵格化 ;
7.異步繪制 ;
*/
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self prepareTableView] ;
}
#pragma mark - prepareTableView
- (void)prepareTableView {
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain] ;
tableView.dataSource = self ;
tableView.delegate = self ;
[tableView registerClass:[MDTableViewCell class] forCellReuseIdentifier:cellIdentifier] ;
tableView.rowHeight = 100 ;
[self.view addSubview:tableView] ;
}
#pragma mark - <UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1 ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 100 ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath] ;
NSURL *url = [NSURL URLWithString:@"http://imglf0.ph.126.net/WAAJPGdYwngSJ5_0nq6YAA==/6608699301143989092.gif"] ;
UIImage *image = [UIImage imageNamed:@"avatar"] ;
[cell.imageView yy_setImageWithURL:url placeholder:image] ;
// [cell.imageView sd_setImageWithURL:url placeholderImage:image] ;
return cell ;
}
/*
要保證表格良好的用戶體驗,刷新幀率應該在50+才是高品質的動畫~人眼識別24幀就已經是較好的動畫效果了!
真機測試要用`舊手機`才能測出性能!
*/
@end
重點關注
**
//Rasterize:柵格化 ;
self.layer.shouldRasterize = YES ;
//但是Rasterize柵格化之后 gif 圖會有模糊的現象,
//所以Rasterize柵格化必須必須指定分辨率!!!
self.layer.rasterizationScale = [UIScreen mainScreen].scale ;
//2.異步繪制:
//UIKit 有一個特點,他的更新都是在主線程里更新的,這也是為什么主線程又被稱為 UI 線程!
//apple 關于異步繪制的介紹非常少:
//這句話加上之后就可以實現異步繪制:
//apple 的官方文檔提到過這個,只說了一句如果 cell 比較復雜可以使用,平時不建議使用~
self.layer.drawsAsynchronously = YES ;**
MDTableViewCell.m 文件
#import "MDTableViewCell.h"
#import "YYWebImage.h"
@implementation MDTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier] ;
if (self) {
//如果不這么寫的話是不能加載動圖的,動圖是不動的 ;
//YYAnimatedImageView 是專門加載動圖用的 ;
//利用 KVC 來修改 imageView 的類型:
/*
@property (nonatomic, "readonly", strong, nullable) UIImageView *imageView NS_AVAILABLE_IOS(3_0); // default is nil. image view will be created if necessary.
給 UITableViewCell 這個類的 imageView 屬性做修改!把 imageView 屬性修改為 YYAnimatedImageView 屬性 ;
imageView 屬性是不能修改的,因為他是只讀屬性~所以只能用 KVC 方法:
*/
[self setValue:[[YYAnimatedImageView alloc] init] forKey:@"imageView"] ;
/**
柵格化:美工術語 , 將 cell 中的所有內容 , 生成一張獨立的圖像 ;
在滾動的過程中 , 我們并沒有點擊 cell 與 cell 交互,所以在滾動時,我們讓 cell 整體變成一張圖像,只顯示這張圖像 ;
*/
//1.下面這兩句話是讓程序性能飛起來的`平凡之路`!!!
//Rasterize:柵格化 ;
self.layer.shouldRasterize = YES ;
//但是Rasterize柵格化之后 gif 圖會有模糊的現象,
//所以Rasterize柵格化必須必須指定分辨率!!!
self.layer.rasterizationScale = [UIScreen mainScreen].scale ;
//2.異步繪制:
//UIKit 有一個特點,他的更新都是在主線程里更新的,這也是為什么主線程又被稱為 UI 線程!
//apple 關于異步繪制的介紹非常少:
//這句話加上之后就可以實現異步繪制:
//apple 的官方文檔提到過這個,只說了一句如果 cell 比較復雜可以使用,平時不建議使用~
self.layer.drawsAsynchronously = YES ;
}
return self ;
}
@end