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

查看:27
本文介绍了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.

人们通常会这样做:

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 的目的是使用更少的内存.如果屏幕可以容纳 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天全站免登陆