iPhone - 什么是reuseIdentifiers(UITableViewCell)? [英] iPhone - What are reuseIdentifiers (UITableViewCell)?

查看:93
本文介绍了iPhone - 什么是reuseIdentifiers(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返回一个单元格。因此,如果要重复使用一个单元格,请确保它对于该情况是正确的。这就是reuseIdentifiers的用途。通常,您只需要一个。但是可能有一个列表使用了几种不同类型的单元格,在这种情况下,您必须通过提供不同的reuseIdentifier来将它们分开。否则,您最终可能会得到一个您视为其他类型单元格的单元格(例如,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).

所以基本上,据我所知,对不同类型的单元使用不同的reuseIdentifier,其中kind表示类。如果您只使用标准单元格,则可能只需要一个reuseIdentifier。

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 - 什么是reuseIdentifiers(UITableViewCell)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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