UITableViewCell的使用

2016-07-11  本文已影响180人  dicesc

(一)系统默认的cell

1. cell 中默认有三个控件

imageView

textLabel

detailTextLabel

cell.accessoryType = 最右侧view显示的样式

cell.accessoryView : 设置的时候, size 是起作用的, x 和 y 设置无效

2.cell 的样式

UITableViewCellStyleDefault, : 图片, textLabel

UITableViewCellStyleValue1, : 三个控件都显示, detailTextLabel 显示在最右侧

UITableViewCellStyleValue2, : 只显示两个label, imageView 不显示了

UITableViewCellStyleSubtitle

(三)cell的重用

tableViewCell 的重用机制

1.原始的方式(基本不用)

根据重用标识符, 到缓存池中查找, 如果找不到, 就重新实例化cell (需要我们手动实现)

1. 定义重用标识符

2. 根据重用标识符到缓存中去找对应的cell

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

3. 对取到的cell 进行判断, 如果找不到就重新实例化cell

实例化的时候, 一定要设置重用标识符 :  identifier

if (nil == cell ) {

cell =  [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];

}


2.注册的方式

1.首先:定义重用标识符 identifier

2.其次:注册

nibWithNibName : xib 的 文件名称

bundle : 查找xib的目录, 传递为nil , 默认就是当前app的安装目录

UINib *nib = [UINib nibWithNibName:@"GroupnCell" bundle:nil];

[_tabeView registerNib:nib forCellReuseIdentifier: identifier ];

如果找不到, 就重新实例化cell (不需要我们手动实现, 而由系统帮我们做)

3.重用

[tableViewdequeueReusableCellWithIdentifier:identifier]


3.代码示例

1.原始方式

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

// 0.定义重用标识符

staticNSString*identifier =@"heroCell";

// 1.先到缓存池中去找cell根据标识符

UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:identifier];

//UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];

// 2.对取到的cell做判断

if(nil== cell) {

//重新实例化一个cell

cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:identifier];

NSLog(@"------- ");

}

return cell

}

2.注册方式(官方推荐)

- (void)viewDidLoad {

[superviewDidLoad];

[self.tableView  registerClass:[UITableViewCellclass]forCellReuseIdentifier:@"cell"];

}

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

UITableViewCell*cell =[tableViewdequeueReusableCellWithIdentifier:@"cell":indexPath];

cell.textLabel.text=@"你好";

returncell;

}

上一篇下一篇

猜你喜欢

热点阅读