1. 定义静态重用标识符
staticNSString *identifier = @”CELL” ;
2. 根据重用标识符从表视图的重用队列中获取可被重用的单元格
UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier :identifier] ;
3. 如果重用队列里并无可被重用的单元格,则创建单元格对象( 常见于表视图刚开始布局单元格时,这时并没有产生重用 ) ,单元格进入到重用队列后,单元格内容不会被清除,当再次使用时,新赋的值会覆盖之前的值
if ( !_aCell ) {
aCell = [ [ UITableViewCell alloc ] initWithStyle :UITableViewCellStyleDefault reuseIdentifier : identifier ] autorelease] ; }
iOS 6之后新增出队列的方法,前三步的代码可以换成如下的代码
//根据重用标识符在表视图的重用队列中获取可被重用的单元格,如果没有可被重用的单元格,则根据之前为表视图指定的单元格类型创建新的单元格对象并返回
UITableView *aCell = [tableView dequeueReusableCellWithIdentifier : @”CELL” forIndexPath : indexPath ] ;
4. 为单元格提供显示的数据
aCell.textLabel.text = [NSStringstringWithFormat : @”%@-%ld” ,aCell.textlabel.text , indexPath.row ] ;
5. 返回单元格
returnaCell ;