- tableView如何顯示數(shù)據(jù)
- 設(shè)置dataSource數(shù)據(jù)源
- 數(shù)據(jù)源要遵守UITableViewDataSource協(xié)議
- 數(shù)據(jù)源要實現(xiàn)協(xié)議中的某些方法
/**
* 告訴tableView一共有多少組數(shù)據(jù)
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
/**
* 告訴tableView第section組有多少行
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
/**
* 告訴tableView第indexPath行顯示怎樣的cell
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
/**
* 告訴tableView第section組的頭部標(biāo)題
*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
/**
* 告訴tableView第section組的尾部標(biāo)題
*/
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
tableView性能優(yōu)化 - cell的循環(huán)利用方式1
/**
* 什么時候調(diào)用:每當(dāng)有一個cell進(jìn)入視野范圍內(nèi)就會調(diào)用
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 0.重用標(biāo)識
// 被static修飾的局部變量:只會初始化一次创肥,在整個程序運行過程中丧慈,只有一份內(nèi)存
static NSString *ID = @"cell";
// 1.先根據(jù)cell的標(biāo)識去緩存池中查找可循環(huán)利用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 2.如果cell為nil(緩存池找不到對應(yīng)的cell)
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
// 3.覆蓋數(shù)據(jù)
cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row];
return cell;
}
tableView性能優(yōu)化 - cell的循環(huán)利用方式2
- 定義一個全局變量
// 定義重用標(biāo)識
NSString *ID = @"cell";
- 注冊某個標(biāo)識對應(yīng)的cell類型
// 在這個方法中注冊cell
- (void)viewDidLoad {
[super viewDidLoad];
// 注冊某個標(biāo)識對應(yīng)的cell類型
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
}
- 在數(shù)據(jù)源方法中返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.去緩存池中查找cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 2.覆蓋數(shù)據(jù)
cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row];
return cell;
}
tableView性能優(yōu)化 - cell的循環(huán)利用方式3
- 在storyboard中設(shè)置UITableView的Dynamic Prototypes Cell
- 設(shè)置cell的重用標(biāo)識
- 在代碼中利用重用標(biāo)識獲取cell
// 0.重用標(biāo)識
// 被static修飾的局部變量:只會初始化一次干茉,在整個程序運行過程中曹动,只有一份內(nèi)存
static NSString *ID = @"cell";
// 1.先根據(jù)cell的標(biāo)識去緩存池中查找可循環(huán)利用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 2.覆蓋數(shù)據(jù)
cell.textLabel.text = [NSString stringWithFormat:@"cell - %zd", indexPath.row];
return cell;
錯誤將UIViewController當(dāng)做UITableViewController來用
UITableView的常見設(shè)置
// 分割線顏色
self.tableView.separatorColor = [UIColor redColor];
// 隱藏分割線
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// tableView有數(shù)據(jù)的時候才需要分割線
// 開發(fā)小技巧:快速取消分割線
self.tableView.tableFooterView = [[UIView alloc] init];
UITableViewCell的常見設(shè)置
// 取消選中的樣式(常用) 讓當(dāng)前 cell 按下無反應(yīng)
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// 設(shè)置選中的背景色
UIView *selectedBackgroundView = [[UIView alloc] init];
selectedBackgroundView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = selectedBackgroundView;
// 設(shè)置默認(rèn)的背景色
cell.backgroundColor = [UIColor blueColor];
// 設(shè)置默認(rèn)的背景色
UIView *backgroundView = [[UIView alloc] init];
backgroundView.backgroundColor = [UIColor greenColor];
cell.backgroundView = backgroundView;
// backgroundView的優(yōu)先級 > backgroundColor
// 設(shè)置指示器
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.accessoryView = [[UISwitch alloc] init];
自定義cell
-
等高的cell
-
storyboard自定義cell
- 1.創(chuàng)建一個繼承自UITableViewCell的子類描沟,比如XMGDealCell
- 1.創(chuàng)建一個繼承自UITableViewCell的子類描沟,比如XMGDealCell
-
- 2.在storyboard中
- 往cell里面增加需要用到的子控件<br>
- 設(shè)置cell的重用標(biāo)識<br>
- 設(shè)置cell的class為XMGDealCell<br>
- 3.在控制器中
- 利用重用標(biāo)識找到cell
- 給cell傳遞模型數(shù)據(jù)<br>
- 4.在XMGDealCell中
- 將storyboard中的子控件連線到類擴(kuò)展中<br>
![](images/Snip20150602_299.png)
- 需要提供一個模型屬性洁桌,重寫模型的set方法荤牍,在這個方法中設(shè)置模型數(shù)據(jù)到子控件上<br>
![](images/Snip20150602_298.png)
![](images/Snip20150602_300.png)
- `xib自定義cell`
- 1.創(chuàng)建一個繼承自UITableViewCell的子類尸折,比如XMGDealCell<br>
- 2.創(chuàng)建一個xib文件(文件名建議跟cell的類名一樣)啰脚,比如XMGDealCell.xib
- 拖拽一個UITableViewCell出來
- 修改cell的class為XMGDealCell
- **設(shè)置cell的重用標(biāo)識**
- 往cell中添加需要用到的子控件
- 3.在控制器中
- **利用registerNib...方法注冊xib文件**
- 利用重用標(biāo)識找到cell(如果沒有注冊xib文件,就需要手動去加載xib文件)
- 給cell傳遞模型數(shù)據(jù)<br>
- 4.在XMGDealCell中
- 將xib中的子控件連線到類擴(kuò)展中
- 需要提供一個模型屬性实夹,重寫模型的set方法橄浓,在這個方法中設(shè)置模型數(shù)據(jù)到子控件上
- 也可以將創(chuàng)建獲得cell的代碼封裝起來(比如cellWithTableView:方法)
- `代碼自定義cell(使用frame)`
- 1.創(chuàng)建一個繼承自UITableViewCell的子類,比如XMGDealCell
- 在initWithStyle:reuseIdentifier:方法中
- 添加子控件
- 設(shè)置子控件的初始化屬性(比如文字顏色亮航、字體)
- 在layoutSubviews方法中設(shè)置子控件的frame
- 需要提供一個模型屬性荸实,重寫模型的set方法,在這個方法中設(shè)置模型數(shù)據(jù)到子控件
- 2.在控制器中
- 利用registerClass...方法注冊XMGDealCell類
- 利用重用標(biāo)識找到cell(如果沒有注冊類缴淋,就需要手動創(chuàng)建cell)
- 給cell傳遞模型數(shù)據(jù)
- 也可以將創(chuàng)建獲得cell的代碼封裝起來(比如cellWithTableView:方法)
- `代碼自定義cell(使用autolayout)`
- 1.創(chuàng)建一個繼承自UITableViewCell的子類准给,比如XMGDealCell
- 在initWithStyle:reuseIdentifier:方法中
- 添加子控件
- 添加子控件的約束(建議使用`Masonry`)
- 設(shè)置子控件的初始化屬性(比如文字顏色、字體)
- 需要提供一個模型屬性重抖,重寫模型的set方法露氮,在這個方法中設(shè)置模型數(shù)據(jù)到子控件
- 2.在控制器中
- 利用registerClass...方法注冊XMGDealCell類
- 利用重用標(biāo)識找到cell(如果沒有注冊類,就需要手動創(chuàng)建cell)
- 給cell傳遞模型數(shù)據(jù)
- 也可以將創(chuàng)建獲得cell的代碼封裝起來(比如cellWithTableView:方法)
- 非等高的cell
xib自定義cell
storyboard自定義cell
代碼自定義cell(frame)
代碼自定義cell(Autolayout)