简体   繁体   English

为什么将“viewWithTag”与“dequeueReusableCellWithIdentifier”一起使用?

[英]Why use “viewWithTag” with “dequeueReusableCellWithIdentifier”?

can someone please explain why you should use viewWithTag to get subviews (eg UILabel etc) from a cell in dequeueReusableCellWithIdentifier ? 有人可以解释为什么你应该使用viewWithTagdequeueReusableCellWithIdentifier的单元格获取子视图(例如UILabel等)?

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). 一些背景信息:我有一个带有几个UILabel的自定义UITableViewCell (我在下面复制了一个简单的版本)。 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. 这些标签在关联的NIB文件中定义,并使用IBOutlet声明并链接回自定义单元的控制器类。 In the tableview's dequeueReusableCellWithIdentifier , I'm doing this: 在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! 谢谢!

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 simple cases this is an acceptable solution, that's what viewWithTag: was intended for. 对于非常简单的情况,这是一个可接受的解决方案,这就是viewWithTag:用途。 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. 但是,如果您要重复使用该单元格,或者希望它具有更易于开发人员的界面,那么您将需要子类化并使用第一个示例中的实际属性。

So use viewWithTag: if it's a very simple cell you designed in IB with no subclass and with just a couple of labels. 所以使用viewWithTag:如果它是你在IB中设计的一个非常简单的单元格,没有子类,只有几个标签。 Use a cell subclass with real properties for anything more substantial. 使用具有真实属性的单元子类来获得更实质的内容。

我已经意识到,如果元素以编程方式添加到单元格中(即未在NIB中定义并通过IBOutlets连接),则使用“viewWithTag”检索元素很有用 - 这样可以防止为每个实例创建多个标签等细胞

For me , viewWithTag is a God given. 对我来说,viewWithTag是上帝赐予的。 First of all : treating all views in a loop like taskinoor said is really easy. 首先:处理像taskinoor这样的循环中的所有视图说真的很容易。 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. 反对xib方法,你必须查看代码和xib。 Also , if you have an offscreen view in a xib , you might oversee it. 此外,如果你在xib中有一个屏幕外视图,你可能会监督它。 I found a lot of xibs made by other programmers that were FULL with lots and lots of views. 我发现很多其他程序员制作的xib都很多,有很多很多观点。 Some hidden , some offscreen , couldn't tell which is which since there were all overlapping. 一些隐藏的,一些屏外的,无法分辨哪个是因为所有重叠。 In those cases , I think xibs are bad. 在那些情况下,我认为xib是坏的。 They are not easy to read anymore. 它们不再容易阅读。 I prefer everything made in code. 我更喜欢代码中的所有内容。

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定义列表以保持代码清洁和可读。

I always hook subviews to properties of my UITableViewCell subclass via IBOutlets, as you have done. 我总是通过IBOutlets将子视图挂钩到我的UITableViewCell子类的属性,就像你所做的那样。 I can't think of any good reason to use viewWithTag. 我想不出任何使用viewWithTag的好理由。

From UITableViewCell Class Reference: "The table view's delegate in tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell." 来自UITableViewCell类参考:“tableView中的表视图委托:cellForRowAtIndexPath:重复使用单元格时应始终重置所有内容。” Keep it simple, clear out the content view. 保持简单,清除内容视图。 This makes no assumptions about custom cell classes, no casts, no class inspection: 这不会假设自定义单元格类,没有强制转换,没有类检查:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell != nil)
{
    NSArray* contentSubViews = [cell.contentView subviews];
    for (UIView* viewToRemove in contentSubViews)
    {
        [viewToRemove removeFromSuperview];
    }
}

viewWithTag: allows styling without creating a custom subclass of 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. 您可以在Interface Builder中为标记UITableViewCell分配标记和重用标识符,然后在UITableViewController的实现中使用该标记对视图进行出列和修改,而无需为该单元格创建自定义类或为单元格的子视图创建IBOutlet。

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项目添加额外的类文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM