ios 将项目添加到 storyboard-IB 中的原型单元格时出现奇怪的错误

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

Strange error when adding items to prototype cells in storyboard-IB

iosuitableviewiboutlet

提问by Alex Godbehere

I have quite a large project (~20 scenes). One of which is a TableViewController with a custom UITableViewController class. I have given the cell a reuse identifier, and added a label to it. When I try and Ctrl+Drag the label to the UITableViewController header file to create an outlet, I get the following error:

我有一个相当大的项目(约 20 个场景)。其中之一是带有自定义 UITableViewController 类的 TableViewController。我为单元格提供了一个重用标识符,并为其添加了一个标签。当我尝试 Ctrl+Drag 标签到 UITableViewController 头文件以创建插座时,出现以下错误:

error: Illegal Configuration: Connection "tableInfoView" cannot have a prototype object as its destination.

错误:非法配置:连接“tableInfoView”不能将原型对象作为其目标。

What is this? Am I overlooking something obvious? Or do I need to create a custom cell class and drag the outlet to that? If so, how do I then specify the data which is displayed uniquely for each cell from the UITableViewController?

这是什么?我是否忽略了一些明显的东西?或者我是否需要创建一个自定义单元格类并将出口拖到那里?如果是这样,我该如何指定 UITableViewController 中每个单元格唯一显示的数据?

回答by dulgan

In fact you can't just make an outlet from a dynamic cell prototype in the UITableViewdelegate view controller.

实际上,您不能仅从UITableView委托视图控制器中的动态单元格原型制作插座。

You'll have to subclass UITableViewCelland then attribute this class to your prototype.

您必须子类化UITableViewCell,然后将此类归因于您的原型。

Then you can Ctrl-Drag from the Label to the UITableViewCellsubclass header file.

然后你可以 Ctrl-Drag 从 Label 到UITableViewCell子类头文件。

Finaly you can access to this outlet in the delegate code after having imported the UITableViewCellheader file in it.

最后,在导入UITableViewCell头文件后,您可以在委托代码中访问此插座。

This is documented by Apple thereat "The Technique for Dynamic Row Content" section.

这是苹果记载以“技术为动态记录内容”部分。

回答by Martin Lockett

or you could give the label a tag (e.g. 100) and use

或者你可以给标签一个标签(例如 100)并使用

myLabel = [myTableView viewForTag:100];

to get the label

获取标签

回答by Reaper

Tag the label and you can reach the label anywhere in the viewcontroller like with viewWithTag from the table view.

标记标签,您可以在视图控制器中的任何位置到达标签,就像从表视图中使用 viewWithTag 一样。

    UILabel *destinationLabel = (UILabel *)[self.tableView viewWithTag:1];


    destinationLabel.text = @"Label Destaination";

回答by Hermann Klecker

I had the same error myself. Just to add one more potantial root cause for future readers:

我自己也有同样的错误。只是为未来的读者增加一个潜在的根本原因:

In my case I copied a control (a Button in this case) from one prototype cell to the next and the action still referred to the neighbor cell. My table has several different prototype cells.

在我的例子中,我将一个控件(在本例中为 Button)从一个原型单元格复制到下一个单元格,并且该操作仍然指向相邻单元格。我的表有几个不同的原型单元格。

The fact, that it acutally was a proper subclass of UITableViewCell which was properly connected to the prototype cell made it difficult to actually see the mistake.

事实上,它实际上可以是这是正确连接到原型细胞使其难以真正看到错误的UITableViewCell的一个适当的子类。

回答by thesummersign

I faced the same problem but later it turned out that it was just a silly mistake. I mistakenly dragged the label from Cell to my controller's @interface

我遇到了同样的问题,但后来证明这只是一个愚蠢的错误。 我错误地将标签从 Cell 拖到了控制器的@interface

This could be your problem too. just cross check once.

这也可能是你的问题。只需交叉检查一次。

回答by Wodjefer

Set the right reuse identifier used in .m file in the Storyboard for the Prototype cell.I had the same situation and this helped me

为原型单元的情节提要中的 .m 文件设置正确的重用标识符。我遇到了同样的情况,这对我有帮助

回答by Shahid Aslam

After doing every thing right if problem still exist then just removed all outlets and rejoin them carefully and it worked very fine for me.

如果问题仍然存在,那么在做正确的每一件事之后,只需移除所有插座并小心地重新加入它们,它对我来说效果很好。