ios 在故事板中连接 Cell Prototype 的出口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10176312/
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-08-30 17:53:30  来源:igfitidea点击:

Connect outlet of a Cell Prototype in a storyboard

iosuitableviewstoryboardiboutlet

提问by sebastien

I'm a newbie with the Storyboard and so I have some difficulties...

我是 Storyboard 的新手,所以我遇到了一些困难......

I have created a TableViewController and I would like to customize the Cell Prototype. In the Cell Prototype I have added several Labels I would like to customize with my own class which inherits from UITableViewCell (AreaListCell). In the Storyboard, for the Cell Prototype I have configured the Custom Class with "AreaListCell" and its style is "Custom".

我已经创建了一个 TableViewController,我想自定义 Cell Prototype。在 Cell Prototype 中,我添加了几个标签,我想用我自己的类进行自定义,这些类继承自 UITableViewCell (AreaListCell)。在 Storyboard 中,对于 Cell Prototype,我已将自定义类配置为“AreaListCell”,其样式为“Custom”。

In the storyboard, when I select the Cell Prototype and then the assistant, the assistant display my class that implements the UITableViewController (AreasTableViewController) and not
my "AreaListCell" class.

在情节提要中,当我选择 Cell Prototype 然后选择助手时,助手会显示我实现 UITableViewController (AreasTableViewController) 的类,而不是
我的“AreaListCell”类。

The consequence is I can create outlet (using Ctrl + Drag from the label of the Cell Prototype) to the AreasTableViewController class but not to the AreaListCell class ! Any idea how to connect the Cell Prototype with my AreaListCell class?

结果是我可以创建出口(使用 Ctrl + 从 Cell Prototype 的标签拖动)到 AreasTableViewController 类,但不能创建到 AreaListCell 类!知道如何将 Cell Prototype 与我的 AreaListCell 类连接吗?

Thanks for your help!

谢谢你的帮助!

回答by jrturton

UPDATE: As of Xcode 4.6 (possibly earlier) you can now create outlets by control-dragging! - This has to be done into an interface section or class extension (the class extension doesn't exist by default for new cell subclasses. Thanks to Steve Haley for pointing this out.

更新:从 Xcode 4.6(可能更早)开始,您现在可以通过控制拖动来创建插座!- 这必须在接口部分或类扩展中完成(默认情况下,新单元子类的类扩展不存在。感谢 Steve Haley 指出这一点。

You can't get the outlet automatically connected and created by dragging into the code block in the assistant editor, which is poor, but you can create the outlets manually and connect them then.

不能通过在助手编辑器中拖入代码块来自动连接和创建插座,这很糟糕,但您可以手动创建插座然后连接它们。

In your cell subclass interface:

在您的单元格子类界面中:

@interface CustomCell : UITableViewCell

@property (nonatomic) IBOutlet UILabel* customLabel;

@end

Synthesize as normal in the implementation.

在实现中正常合成。

In the storyboard, select the cell and go to the connections inspector, you will see the new outlet. Drag from there to the relevant element in your prototype:

在故事板中,选择单元格并转到连接检查器,您将看到新的插座。从那里拖到原型中的相关元素:

enter image description here

在此处输入图片说明

This can now be accessed as cell.customLabelin your cellForRowAtIndexPath:method.

现在可以像cell.customLabel在您的cellForRowAtIndexPath:方法中一样访问它。

回答by Justin Paulson

Yeah you can't connect views that are inside of a custom prototype cell using the ctrl+drag method. Instead use the tag property of the view and then when you are building the cell pull the labels out using their tags.

是的,您无法使用 ctrl+drag 方法连接自定义原型单元格内的视图。而是使用视图的 tag 属性,然后在构建单元格时使用它们的标签拉出标签。

Here:

这里:

//Let's assume you have 3 labels.  One for a name, One for a count, One for a detail
//In your storyboard give the name label tag=1, count tag=2, and detail tag=3


- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomTableViewCell *theCell = [tableView dequeueReusableCellWithIdentifier:@"Prototype Cell"];

    UILabel *nameLabel = (UILabel *)[theCell viewWithTag:1];
    UILabel *countLabel = (UILabel *)[theCell viewWithTag:2];
    UILabel *detailLabel = (UILabel *)[theCell viewWithTag:3];

    nameLabel.text = @"name";
    countLabel.text = @"count";
    detailLabel.text = @"details";

    return theCell;
}

You could also set the labels up as properties in your custom cell code and then when the cell is initialized use the viewWithTag call to assign the label properties to the labels you have created on your storyboards.

您还可以在自定义单元格代码中将标签设置为属性,然后在单元格初始化时使用 viewWithTag 调用将标签属性分配给您在故事板上创建的标签。

It took me a few days to realize I couldn't ctrl+drag from inside a custom cell to create an IBOutlet.

我花了几天的时间才意识到我无法从自定义单元格内部按住 ctrl+拖动来创建 IBOutlet。

Good luck!

祝你好运!

EDIT: You CAN create IBOutlets for your labels inside of a custom cell and create the links programatticaly, just not through the ctrl+drag method.

编辑:您可以在自定义单元格内为您的标签创建 IBOutlets 并以编程方式创建链接,而不是通过 ctrl+drag 方法。

EDIT 2: I was totally wrong, you can ctrl+drag. See the second answer to this question. It is tricky, but it works quite well.

编辑 2:我完全错了,你可以按 ctrl+drag。请参阅此问题的第二个答案。这很棘手,但效果很好。

回答by Ameer Chand

Swift 3

斯威夫特 3

// we are using this if your images are at server.

// 如果您的图像在服务器上,我们将使用它。

// we are getting images from a url.

// 我们从一个 url 获取图像。

// you can set image from your Xcode.

// 您可以从 Xcode 设置图像。

  1. The URL of images are in an array name = thumbnail i.e self.thumbnail[indexPath.row]
  2. on UITableviewCell put a imageView on cell
  3. select UIimageView assign it a tag from storyboard.

    let pictureURL = URL(string: self.thumbnail[indexPath.row])!
    let pictureData = NSData(contentsOf: pictureURL as URL)
    let catPicture = UIImage(data: pictureData as! Data)
    var imageV = UIImageView()
    imageV = cell?.viewWithTag(1) as! UIImageView
    imageV.image = catPicture
    
  1. 图像的 URL 在一个数组名称 = 缩略图中,即 self.thumbnail[indexPath.row]
  2. 在 UITableviewCell 上在单元格上放一个 imageView
  3. 选择 UIimageView 为其分配故事板中的标签。

    let pictureURL = URL(string: self.thumbnail[indexPath.row])!
    let pictureData = NSData(contentsOf: pictureURL as URL)
    let catPicture = UIImage(data: pictureData as! Data)
    var imageV = UIImageView()
    imageV = cell?.viewWithTag(1) as! UIImageView
    imageV.image = catPicture