cell筆記 <筆記篇>

## tableView性能優(yōu)化- cell的循環(huán)利用方式1

```objc

/**

*什么時(shí)候調(diào)用:每當(dāng)有一個(gè)cell進(jìn)入視野范圍內(nèi)就會(huì)調(diào)用

*/

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

{

// 0.重用標(biāo)識(shí)

//被static修飾的局部變量:只會(huì)初始化一次,在整個(gè)程序運(yùn)行過程中,只有一份內(nèi)存

static NSString *ID = @"cell";

// 1.先根據(jù)cell的標(biāo)識(shí)去緩存池中查找可循環(huán)利用的cell

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

// 2.如果cell為nil(緩存池找不到對(duì)應(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

-定義一個(gè)全局變量

```objc

//定義重用標(biāo)識(shí)

NSString *ID = @"cell";

```

-注冊(cè)某個(gè)標(biāo)識(shí)對(duì)應(yīng)的cell類型

```objc

//在這個(gè)方法中注冊(cè)cell

- (void)viewDidLoad {

[super viewDidLoad];

//注冊(cè)某個(gè)標(biāo)識(shí)對(duì)應(yīng)的cell類型

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];

}

```

-在數(shù)據(jù)源方法中返回cell

```objc

- (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

![](images/Snip20150602_152.png)

-設(shè)置cell的重用標(biāo)識(shí)

![](images/Snip20150602_153.png)

-在代碼中利用重用標(biāo)識(shí)獲取cell

```objc

// 0.重用標(biāo)識(shí)

//被static修飾的局部變量:只會(huì)初始化一次,在整個(gè)程序運(yùn)行過程中,只有一份內(nèi)存

static NSString *ID = @"cell";

// 1.先根據(jù)cell的標(biāo)識(shí)去緩存池中查找可循環(huán)利用的cell

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

// 2.覆蓋數(shù)據(jù)

cell.textLabel.text = [NSString stringWithFormat:@"cell - %zd", indexPath.row];

return cell;

```

##錯(cuò)誤將UIViewController當(dāng)做UITableViewController來用

![](images/Snip20150602_110.png)

## UITableView的常見設(shè)置

```objc

//分割線顏色

self.tableView.separatorColor = [UIColor redColor];

//隱藏分割線

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

// tableView有數(shù)據(jù)的時(shí)候才需要分割線

//開發(fā)小技巧:快速取消分割線

self.tableView.tableFooterView = [[UIView alloc] init];

```

## UITableViewCell的常見設(shè)置

```objc

//取消選中的樣式(常用)讓當(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)先級(jí)> backgroundColor

//設(shè)置指示器

//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

cell.accessoryView = [[UISwitch alloc] init];

```

##自定義cell

- `等高的cell`

- `storyboard自定義cell`

- 1.創(chuàng)建一個(gè)繼承自UITableViewCell的子類,比如XMGDealCell

![](images/Snip20150602_305.png)

- 2.在storyboard中

-往cell里面增加需要用到的子控件

![](images/Snip20150602_302.png)

-設(shè)置cell的重用標(biāo)識(shí)

![](images/Snip20150602_303.png)

-設(shè)置cell的class為XMGDealCell

![](images/Snip20150602_304.png)

- 3.在控制器中

-利用重用標(biāo)識(shí)找到cell

-給cell傳遞模型數(shù)據(jù)

![](images/Snip20150602_301.png)

- 4.在XMGDealCell中

-將storyboard中的子控件連線到類擴(kuò)展中

![](images/Snip20150602_299.png)

-需要提供一個(gè)模型屬性,重寫模型的set方法,在這個(gè)方法中設(shè)置模型數(shù)據(jù)到子控件上

![](images/Snip20150602_298.png)

![](images/Snip20150602_300.png)

- `xib自定義cell`

- 1.創(chuàng)建一個(gè)繼承自UITableViewCell的子類,比如XMGDealCell

- 2.創(chuàng)建一個(gè)xib文件(文件名建議跟cell的類名一樣),比如XMGDealCell.xib

-拖拽一個(gè)UITableViewCell出來

-修改cell的class為XMGDealCell

- **設(shè)置cell的重用標(biāo)識(shí)**

-往cell中添加需要用到的子控件

- 3.在控制器中

- **利用registerNib...方法注冊(cè)xib文件**

-利用重用標(biāo)識(shí)找到cell(如果沒有注冊(cè)xib文件,就需要手動(dòng)去加載xib文件)

-給cell傳遞模型數(shù)據(jù)

- 4.在XMGDealCell中

-將xib中的子控件連線到類擴(kuò)展中

-需要提供一個(gè)模型屬性,重寫模型的set方法,在這個(gè)方法中設(shè)置模型數(shù)據(jù)到子控件上

-也可以將創(chuàng)建獲得cell的代碼封裝起來(比如cellWithTableView:方法)

- `代碼自定義cell(使用frame)`

- 1.創(chuàng)建一個(gè)繼承自UITableViewCell的子類,比如XMGDealCell

-在initWithStyle:reuseIdentifier:方法中

-添加子控件

-設(shè)置子控件的初始化屬性(比如文字顏色、字體)

-在layoutSubviews方法中設(shè)置子控件的frame

-需要提供一個(gè)模型屬性,重寫模型的set方法,在這個(gè)方法中設(shè)置模型數(shù)據(jù)到子控件

- 2.在控制器中

-利用registerClass...方法注冊(cè)XMGDealCell類

-利用重用標(biāo)識(shí)找到cell(如果沒有注冊(cè)類,就需要手動(dòng)創(chuàng)建cell)

-給cell傳遞模型數(shù)據(jù)

-也可以將創(chuàng)建獲得cell的代碼封裝起來(比如cellWithTableView:方法)

- `代碼自定義cell(使用autolayout)`

- 1.創(chuàng)建一個(gè)繼承自UITableViewCell的子類,比如XMGDealCell

-在initWithStyle:reuseIdentifier:方法中

-添加子控件

-添加子控件的約束(建議使用`Masonry`)

-設(shè)置子控件的初始化屬性(比如文字顏色、字體)

-需要提供一個(gè)模型屬性,重寫模型的set方法,在這個(gè)方法中設(shè)置模型數(shù)據(jù)到子控件

- 2.在控制器中

-利用registerClass...方法注冊(cè)XMGDealCell類

-利用重用標(biāo)識(shí)找到cell(如果沒有注冊(cè)類,就需要手動(dòng)創(chuàng)建cell)

-給cell傳遞模型數(shù)據(jù)

-也可以將創(chuàng)建獲得cell的代碼封裝起來(比如cellWithTableView:方法)

-非等高的cell

- xib自定義cell

- storyboard自定義cell

-代碼自定義cell(frame)

-代碼自定義cell(Autolayout)

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

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

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