UITableView

一.UITableView 基本概述

UITableView 繼承于UIScrollVIew,可以滾動.

UITableView 的每一條數據對應的單元格叫做Cell, 是UITableViewCell的一個對象,繼承于UIView.

UITableView 可以分區顯示, 每一個分區稱為section, 每一行稱為row, 編號都從0開始.

系統提供了一個專門的類來整合section和row, 叫做NSIndexPath.


A部分為UITableVIew的分區,叫做section


B部分為UITableView的每一行,叫做row


section和row代表一個UITableViewCell在UITableView上的位置

二.UITableView的基本使用

/*

UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds ?style:UITableViewStylePlain];

[self.view addSubview:tableView];

[tableView release];

*/

1.UITableView的樣式枚舉

UITableView的初始化方法包含一個UITableViewStyle類型的參數

這是一個枚舉類型,分為兩種UITableViewStylePain,UITableViewStyleGrouped.

UITableViewStylePlain
UITableViewStyleGroup

2.UITableView顯示的相關屬性

? ? ? ? ? ? ? ? ? ? ? ? ?rowHeight ? ?行高

? ? ? ? ? ? ? ? ? ? ? ? ?separatorStyle ? 分隔線樣式

? ? ? ? ? ? ? ? ? ? ? ? ?separatorColor ? 分隔線顏色

? ? ? ? ? ? ? ? ? ? ? ? ?tableHeaderView ? UITableView的置頂視圖

? ? ? ? ? ? ? ? ? ? ? ? ?tableFooterView ? UITableView置底視圖

三.UITableView顯示數據

1.UITableView中有兩個重要的屬性:

@property (nonatomic, weak, nullable) id<UITableViewDataSource> dataSource; ?------顯示數據相關的代理

@property (nonatomic, weak, nullable) id<UITableViewDelegate> delegate; ?------視圖操作相關的代理

2.UITableView代理的實現代碼

(1)簽訂UITableView協議

/*

@interface ?ViewController ?( ) ?<UITableViewDataSource, UITableViewDelegate>

@end

*/

(2)設置當前的ViewController為UITableView代理

/*

@implementation ?ViewController

-(void)viewDidLoad{

[super ?viewDidLoad];

UITableView ?*tableView ?= ?[[UITableView ?alloc] ?initWithFrame:self.view.bounds ?style:UITableViewStylePlain];

// 設置dataSource代理

tableView.dataSource ?= ?self;

// 設置delegate代理

tableView.delegate ?= ?self;

[self.view ?addSubview:tableView];

[tableView ?release];

}

*/

UITableViewDataSource

UITableVIew的DataSource是負責給UITableView對象提供數據的代理,遵循UITableViewDataSource協議

協議中有兩個必須實現的協議方法

UITableViewCell

UITableView的每一個單元格是UITableViewCell類的對象,繼承于UIView.

UITableViewCell默認提供了3個視圖屬性:

3種視圖屬性

四.UITableViewCell的重用機制

UITableView有一個重用機池機制管理cell,目的是使用盡可能少的cell顯示所有數據.

1.UITableView重用cell的流程

(1)當一個cell被滑出屏幕,這個cell會被系統放到相應的重用池中.

(2)當tableView需要顯示一個cell,會先去重用池中嘗試獲取一個cell.

(3)如果重用池沒有cell,就會創建一個cell.

(4)取得cell之后會重新賦值進行使用.

2.UITableView重用cell的代碼流程

(1)在創建UITableView之后,需要注冊一個cell類,當重用池中沒有cell的時候,系統可以自動創建cell. ?相關方法:

/*

-(void)registerClass:(class)cellClass forCellReuseldentifier:(NSString *)identifier;

*/

(2)系統提供了一個獲取重用池中cell的方法(需要提供一個重用標識):

/*

-(UITableViewCell *)dequeueReusableCellWithldentifier:(NSString *)identifier;

*/

3.修改后的UITableView代碼

/*

-(void)viewDidLoad{

[super ?viewDidLoad];

/*創建tableView的代碼*/

// ?參數1: 當重用池沒有cell的時候使用什么類創建Cell

// ?參數2: 這個重用池的標識

[tableView ?registerClass:[UITableViewCell ?class] forCellReuseIdentifier:@"reuse"];

}

*/

4.修改后的UITableViewDataSource協議代碼

// tableView每次要顯示一個cell都會調用這個方法獲取

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

//1.從重用池中取得一個cell, 如果重用池中沒有cell, 系統會根據注冊的cell類自動創建一個返回

UITableViewCell ?*cell ?= ?[tableView ?dequeueReusableCellWithIdentifier:@"reuse"];

//2.給cell上的視圖重新賦值

cell.textLabel.text ?= ?@"標題";

//3.返回cell

return ?cell;

}

五.UITableView和數組數據的結合使用

UITableView每一行顯示的內容不可能都是一樣的.

1.UITableView和數組

/*

@interface ?ViewController ( ) <UITableViewDataSource, UITableViewDelegate>

// 數組屬性, 用來和tableView結合使用

@property (nonatomic, retain) NSMutableArray ?*sourceArr;

@end

@implementation ?ViewController

- (void)viewDidLoad {

[super ?viewDidLoad];

// 初始化數組

self.sourceArr ?= ?[NSMutableArray ?arrayWithObjects:@"張三",@"李四",@"王五",@"趙六",nil];

}

2.UITableView結合數組的代碼(1)

// tableView每個分區要顯示的行數

- (NSInteger) tableView:(UITableVIew *)tableView numberOfRowsInSection:(NSInteger)section{

// 根據元素個數設置行數

return ?self.sourceArr.count;

}

// tableView每次要顯示一個cell都會調用這個方法獲取

-(UITableViewCell ?*)tableView:(UITableVIew *)tableView cellForRowAtIndexpath:(NSIndexPath *)indexPath{

UITableViewCell ?*cell ?= ?[tableView ?dequeueReusableCellWithIdentifier:@"reuse"];

// 根據row從數組中取值

cell.textLable.text ?= [self.sourceArr ?objectAtIndex:indexPath.row];

return ?cell;

}

六.UITableView的常用協議方法

1.UITableViewDataSource

UITableViewDataSource

2.UITableViewDelegate

UITableViewDelegate
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容