objective-c iPhone - 什么是重用标识符(UITableViewCell)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2152180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:50:28  来源:igfitidea点击:

iPhone - What are reuseIdentifiers (UITableViewCell)?

iphoneobjective-cuitableviewreuseidentifier

提问by quano

From the official documentation:

来自官方文档:

The reuse identifier is associated with a UITableViewCell object that the table-view's delegate creates with the intent to reuse it as the basis (for performance reasons) for multiple rows of a table view. It is assigned to the cell object in initWithFrame:reuseIdentifier: and cannot be changed thereafter. A UITableView object maintains a queue (or list) of the currently reusable cells, each with its own reuse identifier, and makes them available to the delegate in the dequeueReusableCellWithIdentifier: method.

重用标识符与表视图的委托创建的 UITableViewCell 对象相关联,目的是将其重用作为表视图多行的基础(出于性能原因)。它被分配给 initWithFrame:reuseIdentifier: 中的单元格对象,此后无法更改。UITableView 对象维护一个当前可重用单元格的队列(或列表),每个单元格都有自己的重用标识符,并在 dequeueReusableCellWithIdentifier: 方法中将它们提供给委托。

http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/occ/instp/UITableViewCell/reuseIdentifier

http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/occ/instp/UITableViewCell/reuseIdentifier

I don't understand this. Well, I understand the basic idea, I think, that you create UITableViewCells, and try to reuse as many as you can instead of making new ones (or something like that). But what exactly decides whether or not a cell is reusable? If I've got two identical (visually) cells, but with different texts (well I suppose they aren't entirely identical), can they both have the same identifier? Or should they have different ones? Or in what situation are you supposed to use different identifiers?

我不明白这个。好吧,我理解基本思想,我认为,您创建 UITableViewCells,并尝试尽可能多地重用而不是创建新的(或类似的东西)。但是究竟是什么决定了一个单元格是否可重用呢?如果我有两个相同(视觉上)的单元格,但文本不同(我想它们并不完全相同),它们是否可以具有相同的标识符?或者他们应该有不同的?或者在什么情况下你应该使用不同的标识符?

Can anyone clarify or link to a place where it is?

任何人都可以澄清或链接到它所在的地方吗?

回答by quano

Ok, this is how I believe it works:

好的,这就是我相信它的工作原理:

Using dequeueReusableCellWithIdentifier for the tableView, you can greatly speed things up. Instead of instantiating a lot of cells, you just instantiate as many as needed, i.e. as many that are visible (this is handled automatically). If scrolling to an area in the list where there are "cells" that haven't got their visual representation yet, instead of instantiating new ones, you reuse already existing ones.

对 tableView 使用 dequeueReusableCellWithIdentifier,可以大大加快速度。不是实例化很多单元格,您只需根据需要实例化尽可能多的单元格,即尽可能多的可见(这是自动处理的)。如果滚动到列表中存在尚未获得视觉表示的“单元格”的区域,则不是实例化新单元格,而是重用已有的单元格。

You can try this yourself by doing this:

您可以通过以下方式自己尝试:

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

if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    NSLog(@"new one");
}
else
{
    NSLog(@"old one");
}

Remember, you only want dequeueReusableCellWithIdentifier to return a cell if it is applicable. So if a cell is going to be reused, make sure it is correct for the situation. That's what reuseIdentifiers are for. Usually, you will only need one. But there might be a list that uses several different kinds of cells, and in that case, you'd have to keep them separate by providing different reuseIdentifiers. Otherwise you might end up getting a cell that you treat as some other kind of cell (for example, UITableView cell instead of the custom one you wanted).

请记住,如果适用,您只希望 dequeueReusableCellWithIdentifier 返回一个单元格。因此,如果要重复使用一个单元格,请确保它适合这种情况。这就是重用标识符的用途。通常,您只需要一个。但是可能有一个列表使用了几种不同类型的单元格,在这种情况下,您必须通过提供不同的重用标识符来将它们分开。否则,您最终可能会得到一个您将其视为其他类型单元格的单元格(例如, UITableView 单元格而不是您想要的自定义单元格)。

So basically, as I understand it, use different reuseIdentifiers for different kinds of cells, where kind means class. If you only use standard cells, you probably only need one reuseIdentifier.

所以基本上,据我所知,对不同种类的单元格使用不同的重用标识符,其中种类意味着类。如果您只使用标准单元格,您可能只需要一个重用标识符。

This design pattern is known as object pooling.

这种设计模式称为对象池

回答by csotiriou

Just to add some things to quano's otherwise very good answer: (I tried to add this as a comment, but it was too long!)

只是在 quano 的其他非常好的答案中添加一些内容:(我尝试将其添加为评论,但它太长了!)

Even reuse identifiers can be omitted in developing, although this must be done in very specific circumstances. If you have a table view of 6-7 cells, and each one is different, you may find that creating a new cell with nil as the identifier may be preferable.

在开发中甚至可以省略重用标识符,尽管这必须在非常特殊的情况下完成。如果您有一个包含 6-7 个单元格的表格视图,并且每个单元格都不同,您可能会发现创建一个以 nil 作为标识符的新单元格可能更可取。

Having a reusable cell means that in each time the cellForRowAtIndexPath is called, you must check the cell, initialize it if there is no reusable cell, and outside of the init scope you must explicitly iterate through all possible indexpaths and set the values for each label explicitly depending on what kind of cell you have! So, in a table view with 10 dinstinct cells, you will have to take care of creating the cell if nil, and filling it up depending on what you created.

具有可重用单元意味着每次调用 cellForRowAtIndexPath 时,您必须检查该单元,如果没有可重用单元则对其进行初始化,并且在 init 范围之外您必须显式迭代所有可能的索引路径并为每个标签设置值明确地取决于你拥有什么样的细胞!因此,在具有 10 个独特单元格的表格视图中,如果为 nil,您将必须创建单元格,并根据您创建的内容填充它。

Therefore, in this case, it's preferable in terms of code maintenance to initialize each cell with nil identifier (since it's not going to be reused anyway) and fill each cell's info appropriately without worrying about reusing it.

因此,在这种情况下,在代码维护方面,最好用 nil 标识符初始化每个单元格(因为它无论如何都不会被重用)并适当地填充每个单元格的信息而不必担心重用它。

回答by onmyway133

UITableView is like having a cell pool for each reuseIdentifier, so that it recycle the cell

UITableView 就像每个都有一个单元池reuseIdentifier,这样它就可以回收单元

I like this video from http://oleb.net/blog/2014/05/scrollviews-inside-scrollviews/

我喜欢来自http://oleb.net/blog/2014/05/scrollviews-inside-scrollviews/ 的这个视频

http://im.ezgif.com/tmp/ezgif-3302899694.gif

http://im.ezgif.com/tmp/ezgif-3302899694.gif