标签: iOS,苹果开发文档
- dequeueReusableCellWithIdentifier:forIndexPath:
Returns a reusable table-view cell object for the specified reuse identifier and adds it to the table.
Declaration
SWIFT
func dequeueReusableCellWithIdentifier(_ identifier: String,
forIndexPath indexPath: NSIndexPath) -> UITableViewCell
OBJECTIVE-C
- (__kindofUITableViewCell * _Nonnull)dequeueReusableCellWithIdentifier:(NSString * _Nonnull)identifier
forIndexPath:(NSIndexPath * _Nonnull)indexPath ```
## **Parameters**
> **identifier**
A string identifying the cell object to be reused. This parameter must not be nil.
> **indexPath**
The index path specifying the location of the cell. The data source receives this information when it is asked for the cell and should just pass it along. This method uses the index path to perform additional configuration based on the cell’s position in the table view.
指定cell位置的索引路径。当它被调用时,数据源收到这个消息,应该让他通过。这个方法以cell在tableView中的位置使用索引路径去执行附加的配置。
> **Return Value**
A UITableViewCell object with the associated reuse identifier. This method always returns a valid cell.
返回一个有重用标志的tableViewCell对象。这个方法总是返回一个有效的cell。
## **Discussion**
For performance reasons, a table view’��s data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView:cellForRowAtIndexPath: method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse. Call this method from your data source object when asked to provide a new cell for the table view. This method dequeues an existing cell if one is available, or creates a new one based on the class or nib file you previously registered, and adds it to the table.
> 出于性能的原因,一个TableView的数据源一般应该重用tableViewCell对象,当它被分配到每一行的时候。一个TableView保持一个tableViewCell队列或者列表给数据源重用。当被要求提供一个新的cell给TableView,调用这个方法从你的数据源拿到对象。如果有一个可用的cell, 这个方法出队列一个存在的cell,或者基于你以前注册的一个类或者nib文件创建一个新的,并把它添加到table中。
**IMPORTANT**
You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method.
> 也就是使用这个方法前,一定要注册cell
If you registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its initWithStyle:reuseIdentifier: method. For nib-based cells, this method loads the cell object from the provided nib file. If an existing cell was available for reuse, this method calls the cell’s prepareForReuse method instead.
如果你注册了一个指定标识符并且一个新的cell一定被创建的类,这个方法初始化cell会调用 initWithStyle:reuseIdentifier:方法。对于基于nib的cell,这个方法会加载cell对象从提供的nib文件中。如果一个存在的cell是可以被重用的,这个方法会调用cell’s prepareForReuse方法。
## **Availability**
Available in iOS 6.0 and later.