---
title: "[ios]-registerNib:forCellReuseIdentifier:"
author: "119568242"
date: "2013-09-22T14:01:13Z"
tags: []
categories: ["移动开发"]
slug: "ios-registernib-forcellreuseidentifier"
draft: false
lastmod: "2013-09-22T14:01:13Z"
id: 1944854
url: "https://poolo.iteye.com/blog/1944854"
---

#### 关于重用

为了得到高效的View，对于cell的重用是必须的，避免了不断生成和销毁对象的操作，这与在UITableView中的情况是一致的。但值得注意的时，在UICollectionView中，不仅cell可以重用，Supplementary View和Decoration View也是可以并且应当被重用的。在iOS5中，Apple对UITableView的重用做了简化，以往要写类似这样的代码：

```
1
2
3
4
5
6
```

```
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MY_CELL_ID"];
if (!cell) {    //如果没有可重用的cell，那么生成一个 
    cell = [[UITableViewCell alloc] init];
}
//配置cell，blablabla 
return cell
```

而如果我们在TableView向数据源请求数据之前使用`-registerNib:forCellReuseIdentifier:`方法为@“MY\_CELL\_ID”注册过nib的话，就可以省下每次判断并初始化cell的代码，要是在重用队列里没有可用的cell的话，runtime将自动帮我们生成并初始化一个可用的cell。