UITableView

一.UITableView 基本概述

UITableView 繼承于UIScrollVIew,可以滾動.

UITableView 的每一條數(shù)據(jù)對應(yīng)的單元格叫做Cell, 是UITableViewCell的一個對象,繼承于UIView.

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

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


A部分為UITableVIew的分區(qū),叫做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類型的參數(shù)

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

UITableViewStylePlain
UITableViewStyleGroup

2.UITableView顯示的相關(guān)屬性

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

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

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

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

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

三.UITableView顯示數(shù)據(jù)

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

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

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

2.UITableView代理的實現(xiàn)代碼

(1)簽訂UITableView協(xié)議

/*

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

@end

*/

(2)設(shè)置當(dāng)前的ViewController為UITableView代理

/*

@implementation ?ViewController

-(void)viewDidLoad{

[super ?viewDidLoad];

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

// 設(shè)置dataSource代理

tableView.dataSource ?= ?self;

// 設(shè)置delegate代理

tableView.delegate ?= ?self;

[self.view ?addSubview:tableView];

[tableView ?release];

}

*/

UITableViewDataSource

UITableVIew的DataSource是負(fù)責(zé)給UITableView對象提供數(shù)據(jù)的代理,遵循UITableViewDataSource協(xié)議

協(xié)議中有兩個必須實現(xiàn)的協(xié)議方法

UITableViewCell

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

UITableViewCell默認(rèn)提供了3個視圖屬性:

3種視圖屬性

四.UITableViewCell的重用機制

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

1.UITableView重用cell的流程

(1)當(dāng)一個cell被滑出屏幕,這個cell會被系統(tǒng)放到相應(yīng)的重用池中.

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

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

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

2.UITableView重用cell的代碼流程

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

/*

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

*/

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

/*

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

*/

3.修改后的UITableView代碼

/*

-(void)viewDidLoad{

[super ?viewDidLoad];

/*創(chuàng)建tableView的代碼*/

// ?參數(shù)1: 當(dāng)重用池沒有cell的時候使用什么類創(chuàng)建Cell

// ?參數(shù)2: 這個重用池的標(biāo)識

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

}

*/

4.修改后的UITableViewDataSource協(xié)議代碼

// tableView每次要顯示一個cell都會調(diào)用這個方法獲取

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

//1.從重用池中取得一個cell, 如果重用池中沒有cell, 系統(tǒng)會根據(jù)注冊的cell類自動創(chuàng)建一個返回

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

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

cell.textLabel.text ?= ?@"標(biāo)題";

//3.返回cell

return ?cell;

}

五.UITableView和數(shù)組數(shù)據(jù)的結(jié)合使用

UITableView每一行顯示的內(nèi)容不可能都是一樣的.

1.UITableView和數(shù)組

/*

@interface ?ViewController ( ) <UITableViewDataSource, UITableViewDelegate>

// 數(shù)組屬性, 用來和tableView結(jié)合使用

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

@end

@implementation ?ViewController

- (void)viewDidLoad {

[super ?viewDidLoad];

// 初始化數(shù)組

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

}

2.UITableView結(jié)合數(shù)組的代碼(1)

// tableView每個分區(qū)要顯示的行數(shù)

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

// 根據(jù)元素個數(shù)設(shè)置行數(shù)

return ?self.sourceArr.count;

}

// tableView每次要顯示一個cell都會調(diào)用這個方法獲取

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

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

// 根據(jù)row從數(shù)組中取值

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

return ?cell;

}

六.UITableView的常用協(xié)議方法

1.UITableViewDataSource

UITableViewDataSource

2.UITableViewDelegate

UITableViewDelegate
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容