iPhone - 什么是重用标识符 (UITableViewCell)? [英] iPhone - What are reuseIdentifiers (UITableViewCell)?

查看:21
本文介绍了iPhone - 什么是重用标识符 (UITableViewCell)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自官方文档:

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

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.

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

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

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?

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

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

推荐答案

好吧,我相信它是这样工作的:

Ok, this is how I believe it works:

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

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.

您可以通过这样做自己尝试:

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");
}

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

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).

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

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.

这种设计模式被称为对象池.

这篇关于iPhone - 什么是重用标识符 (UITableViewCell)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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