1.UITableView
在iOS中,要實(shí)現(xiàn)表格數(shù)據(jù)展示,最常用的做法就是使用UITableView
UITableView繼承自UIScrollView
,因此支持垂直滾動(dòng),而且性能極佳
UITableView的兩種樣式
UITableViewStylePlain
UITableViewStyleGrouped
如何展示數(shù)據(jù) <UITableViewDataSource>
:
# 1>UITableView需要一個(gè)數(shù)據(jù)源(dataSource)來顯示數(shù)據(jù)
# 2>UITableView會(huì)向數(shù)據(jù)源查詢一共有多少行數(shù)據(jù)以及每一行顯示什么數(shù)據(jù)等
# 3>沒有設(shè)置數(shù)據(jù)源的UITableView只是個(gè)空殼
# 4>凡是遵守 <UITableViewDataSource> 協(xié)議的OC對(duì)象,都可以是UITableView的數(shù)據(jù)源
2.UITableView 數(shù)據(jù)源 <UITableViewDataSource>
tableView展示數(shù)據(jù)的過程
調(diào)用數(shù)據(jù)源的下面方法得知一共有多少組數(shù)據(jù)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
調(diào)用數(shù)據(jù)源的下面方法得知每一組有多少行數(shù)據(jù)
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section;
調(diào)用數(shù)據(jù)源的下面方法得知每一行顯示什么內(nèi)容
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath;
3.UITableViewCell
UITableView的每一行都是一個(gè)UITableViewCell
,通過dataSource的tableView:cellForRowAtIndexPath:
方法來初始化每一行
UITableViewCell
內(nèi)部有個(gè)默認(rèn)的子視圖:contentView
,contentView是UITableViewCell所顯示內(nèi)容的父視圖
,可顯示一些輔助指示視圖
輔助指示視圖
的作用是顯示一個(gè)表示動(dòng)作的圖標(biāo),可以通過設(shè)置UITableViewCell的accessoryType
來顯示,默認(rèn)是
UITableViewCellAccessoryNone(不顯示輔助指示視圖),其他值如下:
UITableViewCellAccessoryDisclosureIndicator
UITableViewCellAccessoryDetailDisclosureButton
UITableViewCellAccessoryCheckmark
還可以通過cell的accessoryView
屬性來自定義輔助指示視圖(比如往右邊放一個(gè)開關(guān))
UITableViewCell的contentView
contentView下默認(rèn)有3個(gè)子視圖
其中2個(gè)是UILabel(通過UITableViewCell的textLabel和detailTextLabel屬性訪問)
第3個(gè)是UIImageView(通過UITableViewCell的imageView屬性訪問)
UITableViewCell還有一個(gè)UITableViewCellStyle
屬性,用于決定使用contentView的哪些子視圖,以及這些子視圖在contentView中的位置
4.UITableViewCell的重用原理
iOS設(shè)備的內(nèi)存有限,如果用UITableView顯示成千上萬條數(shù)據(jù),就需要成千上萬個(gè)UITableViewCell對(duì)象的話,那將會(huì)耗盡iOS設(shè)備的內(nèi)存。要解決該問題,需要重用UITableViewCell對(duì)象
重用原理:
當(dāng)滾動(dòng)列表時(shí),部分UITableViewCell會(huì)移出窗口,
UITableView會(huì)將窗口外的UITableViewCell放入一個(gè)對(duì)象池中,等待重用。
當(dāng)UITableView要求dataSource返回UITableViewCell時(shí),dataSource會(huì)先查看這個(gè)對(duì)象池,
如果池中有未使用的UITableViewCell,dataSource會(huì)用新的數(shù)據(jù)配置這個(gè)UITableViewCell,
然后返回給UITableView,重新顯示到窗口中,從而避免創(chuàng)建新對(duì)象
還有一個(gè)非常重要的問題:有時(shí)候需要自定義UITableViewCell(用一個(gè)子類繼承UITableViewCell),而且每一行用的不一定是同一種UITableViewCell,所以一個(gè)UITableView可能擁有不同類型的UITableViewCell,對(duì)象池中也會(huì)有很多不同類型的UITableViewCell,那么UITableView在重用UITableViewCell時(shí)可能會(huì)得到錯(cuò)誤類型的UITableViewCell
解決方案:
UITableViewCell有個(gè)NSString *reuseIdentifier屬性,
可以在初始化UITableViewCell的時(shí)候傳入一個(gè)特定的字符串標(biāo)識(shí)來設(shè)置
reuseIdentifier(一般用UITableViewCell的類名)。
當(dāng)UITableView要求dataSource返回UITableViewCell時(shí),
先通過一個(gè)字符串標(biāo)識(shí)到對(duì)象池中查找對(duì)應(yīng)類型的UITableViewCell對(duì)象,
如果有,就重用,如果沒有,就傳入這個(gè)字符串標(biāo)識(shí)來初始化一個(gè)UITableViewCell對(duì)象
Cell的重用代碼
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 1.定義一個(gè)cell的標(biāo)識(shí)
static NSString *ID = @”zyxcell";
// 2.從緩存池中取出cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 3.如果緩存池中沒有cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
// 4.設(shè)置cell的屬性...
return cell;
}
5.UITableViewCell自定義 Cell的高度不一致
1.新建一個(gè)繼承自UITableViewCell
的類
2.重寫initWithStyle:reuseIdentifier:
方法
添加所有需要顯示的子控件(不需要設(shè)置子控件的數(shù)據(jù)和frame, 子控件要添加到contentView
中)
進(jìn)行子控件一次性的屬性設(shè)置(有些屬性只需要設(shè)置一次, 比如字體\固定的圖片)
3.提供2個(gè)模型
數(shù)據(jù)模型
: 存放文字?jǐn)?shù)據(jù)\圖片數(shù)據(jù)
frame模型
: 存放數(shù)據(jù)模型\所有子控件的frame\cell的高度
4.cell擁有一個(gè)frame模型(不要直接擁有數(shù)據(jù)模型)
5.重寫frame模型屬性的setter方法: 在這個(gè)方法中設(shè)置子控件的顯示數(shù)據(jù)和frame
6.frame模型數(shù)據(jù)的初始化已經(jīng)采取懶加載
的方式(每一個(gè)cell對(duì)應(yīng)的frame模型數(shù)據(jù)只加載一次
)