插座无法连接到重复内容 iOS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26561461/
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
Outlets cannot be connected to repeating content iOS
提问by Tomblasta
I have just created an app and have started hooking up @IBOutlet's to the storyboard. I am connecting some of them to labels in a UITableViewCell Prototype Cell with a Basic Style. When I connect it though I get this error in the Storyboard:
我刚刚创建了一个应用程序并开始将@IBOutlet 连接到故事板。我将其中一些连接到具有基本样式的 UITableViewCell 原型单元格中的标签。当我连接它时,虽然我在故事板中收到此错误:
The detailText Outlet from the TableViewController to the UILabel is invalid. Outlets cannot be connected to repeating content.
从 TableViewController 到 UILabel 的 detailText Outlet 无效。插座不能连接到重复的内容。
Can someone help me out? I have set it up the way I always do successfully but this time it has chucked me this error.
有人可以帮我吗?我已经按照我一直成功的方式设置了它,但这次它让我犯了这个错误。
回答by Wain
Create a table view cell subclass and set it as the class of the prototype. Add the outlets to that class and connect them. Now when you configure the cell you can access the outlets.
创建一个表格视图单元格子类并将其设置为原型的类。将插座添加到该类并连接它们。现在,当您配置单元时,您可以访问插座。
回答by Fangming
There are two types of table views cells provided to you through the storyboard, they are Dynamic Prototypesand Static Cells
通过故事板提供给您的表格视图单元格有两种类型,它们是动态原型和静态单元格
1. Dynamic Prototypes
1. 动态原型
From the name, this type of cell is generated dynamically. They are controlled through your code, not the storyboard. With help of table view's delegate and data source, you can specify the number of cells, heights of cells, prototype of cells programmatically.
从名字上看,这种类型的单元格是动态生成的。它们是通过您的代码控制的,而不是故事板。借助表格视图的委托和数据源,您可以以编程方式指定单元格数量、单元格高度、单元格原型。
When you drag a cell to your table view, you are declaring a prototype of cells. You can then create any amount of cells base on this prototype and add them to the table view through cellForRow
method, programmatically. The advantage of this is that you only need to define 1 prototype instead of creating each and every cell with all views added to them by yourself (See static cell).
当你将一个单元格拖到你的表格视图中时,你就是在声明一个单元格的原型。然后,您可以基于此原型创建任意数量的单元格,并以cellForRow
编程方式通过方法将它们添加到表格视图中。这样做的好处是您只需要定义 1 个原型,而不是创建每个单元格,并自行添加所有视图(请参阅静态单元格)。
So in this case, you cannot connect UI elements on cell prototype to your view controller. You will have only one view controller object initiated, but you may have many cell objects initiated and added to your table view. It doesn't make sense to connect cell prototype to view controller because you cannot control multiple cells with one view controller connection. And you will get an error if you do so.
因此,在这种情况下,您无法将单元原型上的 UI 元素连接到您的视图控制器。您将只启动一个视图控制器对象,但您可能启动了许多单元格对象并将其添加到您的表视图中。将单元原型连接到视图控制器是没有意义的,因为您无法通过一个视图控制器连接来控制多个单元。如果你这样做,你会得到一个错误。
To fix this problem, you need to connect your prototype label to a UITableViewCell
object. A UITableViewCell
is also a prototype of cells and you can initiate as many cell objects as you want, each of them is then connected to a view that is generated from your storyboard table cell prototype.
要解决此问题,您需要将原型标签连接到一个UITableViewCell
对象。AUITableViewCell
也是单元格的原型,您可以根据需要启动任意数量的单元格对象,然后将每个单元格对象连接到从故事板表格单元格原型生成的视图。
Finally, in your cellForRow
method, create the custom cell from the UITableViewCell
class, and do fun stuff with the label
最后,在您的cellForRow
方法中,从UITableViewCell
类创建自定义单元格,并使用标签做一些有趣的事情
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "yourCellIdentifier") as! YourCell
cell.label.text = "it works!"
return cell
}
2. Static Cells
2. 静态细胞
On the other hand, static cells are indeed configured though storyboard. You have to drag UI elements to each and every cell to create them. You will be controlling cell numbers, heights, etc from the storyboard. In this case, you will see a table view that is exactly the same from your phone compared with what you created from the storyboard. Static cells are more often used for setting page, which the cells do not change a lot.
另一方面,静态单元确实是通过故事板配置的。您必须将 UI 元素拖到每个单元格才能创建它们。您将从情节提要中控制单元格编号、高度等。在这种情况下,您将看到与您从故事板创建的内容完全相同的表格视图。静态单元格更常用于设置页面,单元格变化不大。
To control UI elements for a static cell, you will indeed need to connect them directly to your view controller, and set them up.
要控制静态单元格的 UI 元素,您确实需要将它们直接连接到您的视图控制器,并进行设置。
回答by Justin Domnitz
If you're using a table view to display Settings and other options (like the built-in Settings app does), then you can set your Table View Contentto Static Cellsunder the Attributes Inspector. Also, to do this, you must embedded your Table View in a UITableViewController instance.
如果您使用表格视图来显示设置和其他选项(如内置设置应用程序所做的那样),那么您可以在 Attributes Inspector下将表格视图内容设置为静态单元格。此外,要做到这一点,您必须将 Table View 嵌入到 UITableViewController 实例中。
回答by SLN
Or you don't have to use IBOutletto refer to the object in the view. You can give the Label in the tableViewCella Tag value, for example set the Tag to 123(this can be done by the attributes inspector). Then you can access the label by
或者您不必使用IBOutlet来引用视图中的对象。您可以给tableViewCell 中的 Label一个 Tag 值,例如将 Tag 设置为123(这可以由属性检查器完成)。然后您可以通过以下方式访问标签
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "someID", for: indexPath)
let label = cell.viewWithTag(123) as! UILabel //refer the label by Tag
switch indexPath.row {
case 0:
label.text = "Hello World!"
default:
label.text = "Default"
}
return cell
}
回答by lee
With me I have a UIViewcontroller
, and into it I have a tableview with a custom cell on it. I map my outlet of UILabel into UItableviewcell
to the UIViewController
then got the error.
和我一起,我有一个UIViewcontroller
,在里面我有一个带有自定义单元格的表格视图。我将 UILabel 的出口映射UItableviewcell
到UIViewController
然后得到错误。
回答by Nikhil Muskur
As most people have pointed out that subclassingUITableViewCell
solves this issue.
But the reason this not allowed because the prototype cell(UITableViewCell) is defined by Apple and you cannot add any of your own outlets to it.
正如大多数人指出的那样,子类化UITableViewCell
解决了这个问题。但这是不允许的,因为原型单元(UITableViewCell)是由 Apple 定义的,您不能向其中添加任何自己的插座。
回答by automation eng
Click on simulator , Navigate to Window and enable Device Bezels
单击模拟器,导航到 Window 并启用 Device Bezels