from@"http://blog.csdn.net/dadalan/archive/2009/04/27/4129729.aspx"
dequeueReusableCellWithIdentifier:
Returns a reusable table-view cell object located by its identifier.
- (UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier
Parameters
- identifier
-
A string identifying the cell object to be reused. By default, a reusable cell’s identifier is its class name, but you can change it to any arbitrary value.
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 reuseUITableViewCell
objects when it assigns cells to rows in itstableView:cellForRowAtIndexPath:
method. A table view maintains a queue or list ofUITableViewCell
objects that the table view’s delegate has marked for reuse. It marks a cell for reuse by assigning it a reuse identifier when it creates it (that is, in theinitWithFrame:reuseIdentifier:
method of UITableViewCell
). The data source can access specific “template” cell objects in this queue by invoking thedequeueReusableCellWithIdentifier:
method. You can access a cell’s reuse identifier through its reuseIdentifier
property, which is defined by UITableViewCell
.
Availability
- Available in iPhone OS 2.0 and later.
Related Sample Code
Declared In
UITableView.h
dequeueReusableCellWithIdentifier的運行機制猜測
- static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentifier";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#(NSString *)identifier#>]
- if (cell == nil) {
- <#statements#>
- }
書本上講解的很隨意,只是說爲了重複使用,和節省資源的內部機制。
看文檔介紹的也比較籠統,而代碼也只是訪問到頭文件就終止了,所以才臆測一下實現的機制,還請高手們不論對於錯都寫點什麼。其實我還是很疑惑的。
dequeueReusableCellWithIdentifier消息返回的是UITableViewCell對象,即是說這是一個用來獲取UITableViewCell對象的消息,廢話。
之所以不說是初始化一個對象,是因爲它可能返回nil值,所以纔要在下面補充一個如果cell爲nil時的處理過程。
那麼這個方法是不是可以解釋成爲,從一個UITableViewCell對象池中獲取一個以Identifier參數命名的UITableViewCell對象。
如果在資源緊缺的時候,這個池會自動清理多餘的UITableViewCell對象,則可能無法返回對象,但如果資源豐富,則會保存一些UITableViewCell對象,在需要調用的時候迅速的返回,而不用創建.
當TABLE一開始加載的時候。。REUSABLEQUEEE中沒有任何元素。。當TABLE向下滾動時,滑出TABLEVIEW的CELL被加入到隊列中。。下面用到相同的IDENTIFIER的CELL的時候就不用創建,直接 從QUEUE中拿出,修改相應的屬性。。避免重複創建大量相同STYLE的CELL