ios 为什么将“viewWithTag”与“dequeueReusableCellWithIdentifier”一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3632683/
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
Why use "viewWithTag" with "dequeueReusableCellWithIdentifier"?
提问by cravr
can someone please explain why you should use viewWithTag
to get subviews (e.g. UILabel
etc) from a cell in dequeueReusableCellWithIdentifier
?
有人可以解释为什么你应该使用从单元格中viewWithTag
获取子视图(例如UILabel
等)dequeueReusableCellWithIdentifier
?
Some background info: I've got a custom UITableViewCell
with a couple of UILabel
s in it (I've reproduced a simple version of this below). These labels are defined in the associated NIB file and are declared with IBOutlet
s and linked back to the custom cell's controller class. In the tableview's dequeueReusableCellWithIdentifier
, I'm doing this:
一些背景信息:我有一个UITableViewCell
带有几个UILabel
s的自定义(我在下面复制了一个简单的版本)。这些标签在关联的 NIB 文件中定义,并用IBOutlet
s声明并链接回自定义单元的控制器类。在 tableview 中dequeueReusableCellWithIdentifier
,我这样做:
CustomCell *customCell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomCellId"];
if (customCell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"customCell" owner:self options:nil];
for (id oneObject in nib)
if ([oneObject isKindOfClass:[CustomCell class]])
customCell = (CustomCell *)oneObject;
}
customCell.firstLabel.text = @"Hello";
customCell.secondLabel.text = @"World!";
return customCell;
Everything works fine. However from the tutorials I've seen, it looks like when changing the labels' values I should be doing this instead:
一切正常。然而,从我看过的教程来看,在更改标签的值时,我应该这样做:
UILabel *firstLabel = (UILabel *)[customCell.contentView viewWithTag:555];
firstLabel.text = @"Hello";
UILabel *secondLabel = (UILabel *)[customCell.contentView viewWithTag:556];
secondLabel.text = @"World!";
(The labels' tag values have been set in the NIB).
(标签的标签值已在 NIB 中设置)。
Can someone tell me which method is preferred and why?
有人可以告诉我首选哪种方法以及为什么?
Thanks!
谢谢!
采纳答案by cravr
I've realised that it's useful to retrieve elements using "viewWithTag" if the elements were added to the cell programmatically (i.e. not defined in a NIB and hooked-up via IBOutlets)—this prevents multiple labels etc. to be created for each instance of the cell.
我已经意识到,如果元素以编程方式添加到单元格中(即未在 NIB 中定义并通过 IBOutlets 连接),则使用“viewWithTag”检索元素很有用 - 这可以防止为每个实例创建多个标签等的细胞。
回答by Mike Weller
viewWithTag:
is just a quick and dirty way to pull out child views without having to set up IBOutlet properties on the parent, or even without having to create a UITableViewCell subclass.
viewWithTag:
只是拉出子视图的一种快速而肮脏的方法,而无需在父视图上设置 IBOutlet 属性,甚至无需创建 UITableViewCell 子类。
For very simplecases this is an acceptable solution, that's what viewWithTag:
was intended for. However if you are going to reuse that cell a lot or you want it to have a more developer-friendly interface then you will want to subclass and use real properties as in your first example.
对于非常简单的情况下,这是一个可以接受的解决方案,那就是viewWithTag:
意为。但是,如果您要多次重用该单元格,或者希望它具有对开发人员更友好的界面,那么您将需要子类化并使用第一个示例中的真实属性。
So use viewWithTag:
if it's a very simple cell you designed in IB with no subclass and with just a couple of labels. Use a cell subclass with real properties for anything more substantial.
因此,viewWithTag:
如果它是您在 IB 中设计的没有子类且只有几个标签的非常简单的单元格,请使用。使用具有真实属性的单元子类来获得更实质的内容。
回答by George
For me , viewWithTag is a God given. First of all : treating all views in a loop like taskinoor said is really easy. Also , I personally prefer this way because if I take a look on the code and want to see what happens with a view , I simply search for the tag. It's used everywhere the view is handled. Opposed to the xib approach where you have to look in the code and xib too. Also , if you have an offscreen view in a xib , you might oversee it. I found a lot of xibs made by other programmers that were FULL with lots and lots of views. Some hidden , some offscreen , couldn't tell which is which since there were all overlapping. In those cases , I think xibs are bad. They are not easy to read anymore. I prefer everything made in code.
对我来说, viewWithTag 是上帝赐予的。首先:像taskinoor所说的那样在循环中处理所有视图真的很容易。另外,我个人更喜欢这种方式,因为如果我查看代码并想查看视图会发生什么,我只需搜索标签。它在处理视图的任何地方使用。反对你必须查看代码和 xib 的 xib 方法。此外,如果您在 xib 中有一个屏幕外视图,您可以监督它。我发现了很多其他程序员制作的xibs,它们都是FULL的,有很多很多的观点。一些隐藏的,一些屏幕外的,无法分辨哪个是哪个,因为它们都是重叠的。在这些情况下,我认为 xibs 不好。他们不再容易阅读了。我更喜欢用代码制作的一切。
But if you decide to work with tags, remember to avoid hard-coding any tag. Instead make a list of #define definitions to keep the code clean and readable.
但是,如果您决定使用标签,请记住避免对任何标签进行硬编码。而是制作一个#define 定义列表,以保持代码的整洁和可读性。
回答by cduhn
I always hook subviews to properties of my UITableViewCell subclass via IBOutlets, as you have done. I can't think of any good reason to use viewWithTag.
正如您所做的那样,我总是通过 IBOutlets 将子视图挂接到我的 UITableViewCell 子类的属性上。我想不出任何使用 viewWithTag 的好理由。
回答by Heliotropix
From UITableViewCell Class Reference: "The table view's delegate in tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell." Keep it simple, clear out the content view. This makes no assumptions about custom cell classes, no casts, no class inspection:
来自 UITableViewCell 类参考:“tableView:cellForRowAtIndexPath 中表格视图的委托:重用单元格时应始终重置所有内容。” 保持简单,清除内容视图。这对自定义单元格类没有任何假设,没有强制转换,没有类检查:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell != nil)
{
NSArray* contentSubViews = [cell.contentView subviews];
for (UIView* viewToRemove in contentSubViews)
{
[viewToRemove removeFromSuperview];
}
}
回答by pkamb
viewWithTag:
allows styling without creating a custom subclassof UITableViewCell
.
viewWithTag:
允许在不创建自定义子类的情况下设置样式UITableViewCell
。
You can assign a tag and reuse identifier to a prototype UITableViewCell
in Interface Builder, then dequeue and modify the view with that tag within the implementation of your UITableViewController
, without creating a custom class for that cell or creating IBOutlets for the cell's subviews.
您可以UITableViewCell
在 Interface Builder 中为原型分配标签和重用标识符,然后在您的 实现中使用该标签出列和修改视图UITableViewController
,而无需为该单元格创建自定义类或为单元格的子视图创建 IBOutlets。
In some cases, the simplicity of a cell makes a custom class feel like overkill. viewWithTag:
allows you to add custom text and image to a cell in the Storyboard, then set those customizations via code, without adding extra class files to your Xcode project.
在某些情况下,单元格的简单性让自定义类感觉有点矫枉过正。viewWithTag:
允许您将自定义文本和图像添加到 Storyboard 中的单元格,然后通过代码设置这些自定义,而无需向 Xcode 项目添加额外的类文件。