UITableView dequeueReusableCellWithIdentifier Theory [英] UITableView dequeueReusableCellWithIdentifier Theory

查看:99
本文介绍了UITableView dequeueReusableCellWithIdentifier Theory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当苹果为第一部iPhone开发 UITableView 时,他们在滚动时遇到了性能问题。然后,一位聪明的工程师发现原因是对象的分配带来了代价,因此他想出了一种重用单元格的方法。

When apple developed the UITableView for the first iPhone they had a problem in performance when scrolling through it. Then one clever engineer discovered that the cause of this was that allocation of objects comes with a price, so he came up with a way to reuse cells.


对象分配具有性能成本,特别是如果分配必须在短时间内重复发生 - 例如,当
用户滚动表视图时。如果重用单元格而不是分配
new其中,你大大提高了表视图的性能。

"Object allocation has a performance cost, especially if the allocation has to happen repeatedly over a short period—say, when the user scrolls a table view. If you reuse cells instead of allocating new ones, you greatly enhance table-view performance."

来源:iOS参考库

要重复使用您使用的单元格:

To reuse a cell you use:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

现在,我想知道的是,这里究竟发生了什么?如果有一个具有该标识符的单元格并且只返回那个标识符,它是否在TableView中查找?好吧,但是,如果它发送引用而不是分配,我有一个表视图,让我们说4个具有相同标识符的单元格都可见。如何在没有分配的情况下将自身成倍增加到四个实例中?

Now, what I am wondering is, what actually happens here? Does it look in the TableView if there is a cell with that identifier and just returns that one? Well yea duh, but if it sends a reference instead of allocating and I have a table view with let's say 4 cells with the same identifier all visible. How can it multiply itself into four instances without allocating?

我想知道这一点因为我正在构建日历类型组件而且所有单元格只有文本具有相同的结构在变化中。因此,如果我能以某种方式重用我的细胞而不是分配,我想我可能会获得更好的表现。

I want to know this because I am building a calendar type component and all the cells have the same structure only the text within changes. So if I could somehow reuse my cells instead of allocating I think I might get a better performance.

我自己的理论是它分配了四个细胞(仅仅因为它有太)。当一个单元格从屏幕上消失时,它将被放入TableView重用队列中。当需要一个新的单元格时,如果一个具有相同标识符的单元格可用,它会在que中查找,它会在该单元格上调用 prepareForReuse 方法,并将其自身从队列中删除。 / p>

My own theory is that it allocates the four cells (simply because it has too). When a cell disappears from the screen it will be put in the TableView reuse queue. When a new cell is needed it looks in the que if a cell with the same identifier is available, it invokes prepareForReuse method on that cell and it removes itself from the queue.

推荐答案

dequeueReusableCellWithIdentifier:仅返回单元格如果已标记为可以重复使用。这就是为什么在几乎每个 cellForRowAtIndexPath:方法中你会看到像

dequeueReusableCellWithIdentifier: only returns a cell if it has been marked as ready for reuse. This is why in almost every cellForRowAtIndexPath: method you will see something like




UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (nil == cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:CellIdentifier];
}

// Do something to cell

return cell;

实际上,将分配足够的行来填充<$的可见部分c $ c> tableview (加上一两个)。当单元格在屏幕上滚动时,它们将从中删除​​,并标记为准备好重用。随着可用单元格队列的增长,要求出列单元格的行将开始获取单元格到使用,此时您将不再需要分配。

In effect, enough rows will be allocated to fill the visible part of the tableview (plus one or two more). As cells scroll off screen, they are removed from the table and marked as ready for reuse. As the queue of "available cells" grows, your line that asks for a dequeued cell will start obtaining a cell to use, at which point you will not have to allocate anymore.

这篇关于UITableView dequeueReusableCellWithIdentifier Theory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆