dequeueReusableCellWithIdentifier

it2022-05-07  3

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
DrillDownSaveSimpleDrillDownTableSearchTableViewSuiteUICatalog
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

转载于:https://www.cnblogs.com/holyenzou/archive/2012/05/21/2511992.html


最新回复(0)