如果問iOS中最重要的最常用的UI控件是什么,我覺得UITableView當之無愧!似乎所有常規APP都使用到了UITableView。下面就講一講UITableView的常用知識和使用原理及性能優化!
1.簡介
- UITableView故名思議是一種表格控件,是iOS應用中常用的表格控件。常見UITableView如圖:
UITableView繼承于UIScrollview,因此它默認支持垂直滾動(只支持垂直滾動)
UITableView性能極佳,它可以出色的完成我們工作中的很多功能。
UITableView一共有兩種樣式:UITableViewStylePlain 和 UITableViewStyleGroup
效果分別如圖:
UITableViewStylePlain:一種平鋪的效果,并且分組組頭默認有停靠效果;
UITableViewStyleGroup:一種組間分離的效果,每組之間間距較明顯,有明顯分組跡象。
2.數據源
說完了UITableView的簡介,下面來說說他它是如何展示數據的。
- UITableView要展示數據必須設置數據源,沒有數據源(DataSource)它就是一個空殼。
- UITableView會調用數據源方法來查詢一共有多少行數據以及當前顯示什么樣的數據。
- 凡是遵守 UITableViewDelegate的OC對象都可以做UITableView的數據源
UITableView和數據源的關系如下
數據源方法的調用過程
-
調用如下方法,查詢一共有多少組數據
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // Default is 1 if not implemented
-
調用如下方法,查詢每一組一共有多少行數據
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
-
調用如下方法,查詢每一行顯示什么樣的內容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
UITableView還有一些其他的數據源方法如下
/**
* 返回組頭標題
*/
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
/**
* 返回尾標題
*/
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
···
3.UITableViewCell的復用機制
3.1. 返回UITableViewCell的數據源方法的調用
最簡單的返回UITableViewCell的方法如下
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] init];
cell.textLabel.text = [NSString stringWithFormat:@"第 %zd 行",indexPath.row];
return cell;
}
但是實際使用過程中這樣是非常耗費性能的,因為當用戶滑動UITableView的時候,上面方法會瘋狂的調用,瘋狂的創建UITableViewCell。
蘋果對這種情況做了一些性能優化,UITableViewCell在每次創建的時候只會創建當前UI頁面上可見的Cell。當Cell滑動到屏幕外面,當前Cell會被放入到緩存池中,并且可以給Cell設置一個復用標識,當下次使用Cell的時候可以先到緩存池中查找,如果有對應復用標識的Cell就直接拿出來使用,并重新給內容賦值。
所以根據蘋果復用Cell的思路我們在調用返回cell的方法思路應該如下:
- 1.先從緩存池中查找看有沒有可以復用的cell
- 2.如果有就直接用,如果沒有在根據 復用標識 創建
- 3.創建完成之后對原來數據進行覆蓋(防止復用的cell展示異常數據)
所以上面方法從提高性能角度考慮,應該重新寫為下面這樣:
3.2. UITableView性能優化--Cell復用方式1
/**
* 什么時候調用:每當有一個cell進入視野范圍的時候調用
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 0.創建cell復用標識
static NSString *reuseIdentifier = @"cell";
// 1.根據復用標識在復用池中進行查找
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
// 2.判斷如果復用池cell為nil就根據復用標識自己創建
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
// 3.拿到cell進行數據覆蓋
cell.textLabel.text = [NSString stringWithFormat:@"第 %zd 行",indexPath.row];
return cell;
}
3.2. UITableView性能優化--Cell復用方式2
我們可以手動給tableview注冊對應標識的cell,保證從緩存池中能加載到對應標識的cell。
通過代碼注冊
// 注冊cell
static NSString *reuseIdentifier = @"cell";
[tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:reuseIdentifier];
通過Xib注冊
// 注冊cell
static NSString *reuseIdentifier = @"cell"; // 標識可以在Xib中創建
[tableview registerNib:[UINib nibWithNibName:@"對應的Xib名稱" bundle:nil] forCellReuseIdentifier:reuseIdentifier];
在數據源方法中返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.根據復用標識在復用池中進行查找
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
// 2.拿到cell進行數據覆蓋
cell.textLabel.text = [NSString stringWithFormat:@"第 %zd 行",indexPath.row];
return cell;
}
3.3 UITableView性能優化--Cell復用方式3
第三種方式是通過UITableViewController在StoryBoard中創建,見下面:5.UITableViewController
4.UITableView的代理方法
有了數據源方法,tableview就可以正常展示數據。有了對應的代理方法就可以正常監聽tableview的事件操作。
下面列舉一些常用代理方法:
/**
返回行高,可根據不同行返回不同高
@param tableView tableview
@param indexPath 位置
@return 行高
*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
/**
返回估計行高,此方法不進行真實計算,課改變Cell計算高度方法的調用順序,
@param tableView tableview
@param indexPath 位置
@return 行高
*/
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath;
/**
cell的點擊事件處理
@param tableView tableview
@param indexPath 位置
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
/**
cell將要被選中 -- 先需取消選中某個cell才能正常選中新的cell
*/
- (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
/**
cell將要被取消選中
*/
- (nullable NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
···
5.UITableViewController
UITableViewController繼承自UIViewController。可以方便創建有tableview的控制器
UITableViewController默認擁有tableview。在UITableViewController中 self.tableView 就是 self.view
UITableViewController默認繼承 UITableViewDelegate和UITableViewDataSource,所有可以直接實現對應的數據源方法和代理方法即可。
-
如果在StoryBoard中錯誤將UIViewController當做UITableViewController使用會報:加載不到UITableview的錯誤
Snip20170321_10.png -
在StoryBoard中創建UITableViewController并加載cell步驟如下:
拖一個UITableViewController出來
設置initial view controller(程序第一個加載項)
修改對應的類型為自己創建的VC(如果有)
-
設置Dynamic Prototypes動態類型的內容,下面數量至少為1
Snip20170321_11.png -
設置cell的復用標識identfier Snip20170321_12.png
在代碼中直接加載對應cell即可(無需判空/創建)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.會默認去緩存池中進行查找,如果沒有自動去StoryBaord中找
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
// 2.拿到cell進行數據覆蓋
cell.textLabel.text = [NSString stringWithFormat:@"第 %zd 行",indexPath.row];
return cell;
}
```
**通過StoryBoard創建Cell更加方便**
小結
- UITableView是iOS開發中常用的UI控件
- UITableView需要實現數據源方法,來確定自己展示什么內容,沒有數據源的UITableview只是一個空架子
- UITableView可以通過復用UITableViewCell來實現性能優化
- Cell復用的方式有三種(如上)
- UITableViewController來實現帶有tableview的頁面更方便