xcode 无法在 xib 文件中向 UITableView 添加 PrototypeCell

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

Cannot add a PrototypeCell to UITableView inside xib file

iosobjective-ciphonexcodeuitableview

提问by Phan Van Linh

I am new in IOS. I want to create a UITableViewin a xib file instead of my Main.storyboard.
But the problem is when I drag a UITableViewCellin to UITableView, it does not work like in Main.storyboard.
My Main.storyboardnow have many screen so it's very slow when load and also it's hard to find any screen. Therefore, I want to put put UITableViewto xib file.

我是IOS新手。我想UITableView在 xib 文件中创建一个而不是我的Main.storyboard.
但问题是当我将 a 拖入UITableViewCell到时UITableView,它不像 in 那样工作Main.storyboard
Main.storyboard现在有很多屏幕,所以加载时很慢,而且很难找到任何屏幕。因此,我想放入UITableViewxib 文件。

Do I miss something or I make something wrong?
Any help would be appreciated

我错过了什么还是我做错了什么?
任何帮助,将不胜感激

Here is the describe image enter image description here

这是描述图像 在此处输入图片说明

回答by TwoStraws

I'm sorry, but prototype cells are available only in storyboard-based projects. Hopefully you won't have gone too far down the xib approach and can try multiple storyboards instead. If you find them a bit overwhelming, it's OK; they get better –?note that you don't have to have one storyboard for all your view controllers. You can have several, either linked in code or using storyboard references.

抱歉,原型单元仅在基于故事板的项目中可用。希望您不会在 xib 方法上走得太远,而是可以尝试多个故事板。如果你觉得它们有点压倒性,那没关系;它们变得更好了——请注意,您不必为所有视图控制器都配备一个故事板。您可以拥有多个链接,可以在代码中链接,也可以使用故事板引用。

If you want to stick with xibs, another option is to use registerNibin code. So you design your prototype cells in xibs, then register them in code. Not quite as smooth, but it might suit your needs.

如果你想坚持使用xibs,另一种选择是registerNib在代码中使用。所以你在 xibs 中设计你的原型单元,然后在代码中注册它们。不太顺利,但它可能适合您的需求。

回答by Anil solanki

Please check out this process.Follow this and you will get a custom cell to table

请查看此过程。按照此操作,您将获得一个自定义单元格到表格中

Create Table

创建表

Create New Class for Cell

Create New Class for Cell

Make this class subclass of UITableViewCell

Make this class subclass of UITableViewCell

enter image description here

enter image description here

Use this code to create custom cell to show in table

Use this code to create custom cell to show in table

 do not forget to import custom cell

 do not forget to import custom cell

回答by espitia

If you can use a regular UITableViewCell, simply register it.

如果您可以使用常规UITableViewCell,只需注册它。

In viewDidLoad:

viewDidLoad

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "YourCell")

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "YourCell")

In cellForRowAt indexPath:

cellForRowAt indexPath

let cell = metricsTableView.dequeueReusableCell(withIdentifier: "YourCell", for: indexPath)

let cell = metricsTableView.dequeueReusableCell(withIdentifier: "YourCell", for: indexPath)