Creating Table View Cells
- – registerNib:forCellReuseIdentifier:
- – registerClass:forCellReuseIdentifier:
- – dequeueReusableCellWithIdentifier:forIndexPath:
- – dequeueReusableCellWithIdentifier:
registerNib:forCellReuseIdentifier:
Registers a nib object containing a cell with the table view under a specified identifier.
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
Parameters
nib
A nib object that specifies the nib file to use to create the cell. This parameter cannot be nil.
identifier
The reuse identifier for the cell. This parameter must not be nil and must not be an empty string.
Discussion
Prior to dequeueing any cells, call this method or the registerClass:forCellReuseIdentifier: method to tell the table view how to create new cells. If a cell of the specified type is not currently in a reuse queue, the table view uses the provided information to create a new cell object automatically.
If you previously registered a class or nib file with the same reuse identifier, the nib you specify in the nib parameter replaces the old entry. You may specify nil for nib if you want to unregister the nib from the specified reuse identifier.
详细说明:dequeueReusableCellWithIdentifier:
Returns a reusable table-view cell object located by its identifier.
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier
Parameters
identifier
A string identifying the cell object to be reused. This parameter must not be nil.
Return Value
A UITableViewCell object with the associated identifier or nil if no such object exists in the reusable-cell queue.
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 using the class or nib file you previously registered. If no cell is available for reuse and you did not register a class or nib file, this method returns nil.
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.
详细说明:
由于性能原因,在tableview 的tableView:cellForRowAtIndexPath:方法中,当它为每一行指定cell时,它的数据源应该复用UITableViewCell对象。table view保存了包含UITableViewCell对象的队列或者列表,这些UITableViewCell是数据源标记为复用的对象。当要求为table view提供新的cell时,从你的数据源对象中调用这个方法。如果已经存在一个可用的cell,那么这个方法会出列这个cell。如果不存在可用的cell,那么用你先前注册的类或者nib文件创建一个新的cell。如果没有可复用的cell并且你没有注册一个类或者 nib文件,那么这个方法返回nil。
如果你为指定的identifier注册一个类,并且一个新的cell必须被创建,这个方法通过调用 initWithStyle:reuseIdentifier:方法初始化cell。对于基于nib的cell,这个方法从提供的nib文件中加载cell对象,如果一个已经存在的cell可以复用,那么这个方法调用这个cell的prepareForReuse方法。