iPhone - dequeueReusableCellWithIdentifier用法 [英] iPhone - dequeueReusableCellWithIdentifier usage

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

问题描述

我正在开发一款iPhone应用程序,它有一个非常大的UITableView,其数据来自网络,因此我正在尝试优化其创建和使用。

I'm working on a iPhone app which has a pretty large UITableView with data taken from the web, so I'm trying to optimize its creation and usage.

我发现 dequeueReusableCellWithIdentifier 非常有用,但在看到很多使用它的源代码之后,我想知道我对这个函数的用法是不是很好。

I found out that dequeueReusableCellWithIdentifier is pretty useful, but after seeing many source codes using this, I'm wondering if the usage I make of this function is the good one.

以下是人们通常做的事情:

Here is what people usually do:

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

if (cell == nil) {
  cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"];

// Add elements to the cell
return cell;

以下是我的方式:

// The cell row
NSString identifier = [NSString stringWithFormat:@"Cell %d", indexPath.row]; 

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];

if (cell != nil)
  return cell;

cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:identifier];
// Add elements to the cell
return cell;

区别在于人们对每个单元使用相同的标识符,因此只出列一个标识符可以避免分配新的一个。

The difference is that people use the same identifier for every cell, so dequeuing one only avoids to alloc a new one.

对我来说,排队的目的是给每个单元格一个唯一的标识符,所以当应用程序要求它已经显示的单元格时,既没有分配也没有元素必须完成添加。

For me, the point of queuing was to give each cell a unique identifier, so when the app asks for a cell it already displayed, neither allocation nor element adding have to be done.

很好我不知道哪个是最好的,常用方法将表的内存使用率提高到它显示的确切单元格数,虽然我使用的方法似乎有利于速度,因为它保留所有计算单元格,但可能导致大量内存消耗(除非队列有内部限制)。

In fine I don't know which is best, the "common" method ceils the table's memory usage to the exact number of cells it display, whilst the method I use seems to favour speed as it keeps all calculated cells, but can cause large memory consumption (unless there's an inner limit to the queue).

这样使用它我错了吗?或者仅仅取决于他的需求?

Am I wrong to use it this way? Or is it just up to the developer, depending on his needs?

推荐答案

dequeueReusableCellWithIdentifier <的目的/ code>是使用更少的内存。如果屏幕可以容纳4或5个表格单元格,那么重复使用时,即使表格有1000个条目,也只需要在内存中分配4或5个表格单元格。

The purpose of dequeueReusableCellWithIdentifier is to use less memory. If the screen can fit 4 or 5 table cells, then with reuse you only need to have 4 or 5 table cells allocated in memory even if the table has 1000 entries.

第二种方式没有重用。第二种方式没有优势,只使用一组表格单元格。如果您的表有1000个条目,那么您将在内存中分配1000个单元格。如果你打算这样做,你会把它们放在一个数组中,然后用行号索引数组并返回单元格。对于具有固定单元格的小型表格,这可能是一个合理的解决方案,对于动态或大型表格,这不是一个好主意。

In the second way there is no reuse. There is no advantage in the second way over just using an array of table cells. If your table has 1000 entries then you will have 1000 cells allocated in memory. If you are going to do that you would put them in an array and just index the array with the row number and return the cell. For small tables with fixed cells that may be an reasonable solution, for dynamic or large tables it is not a good idea.

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

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