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
Cannot add a PrototypeCell to UITableView inside xib file
提问by Phan Van Linh
I am new in IOS. I want to create a UITableView
in a xib file instead of my Main.storyboard
.
But the problem is when I drag a UITableViewCell
in to UITableView
, it does not work like in Main.storyboard
.
My Main.storyboard
now 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 UITableView
to xib file.
我是IOS新手。我想UITableView
在 xib 文件中创建一个而不是我的Main.storyboard
.
但问题是当我将 a 拖入UITableViewCell
到时UITableView
,它不像 in 那样工作Main.storyboard
。
我Main.storyboard
现在有很多屏幕,所以加载时很慢,而且很难找到任何屏幕。因此,我想放入UITableView
xib 文件。
Do I miss something or I make something wrong?
Any help would be appreciated
我错过了什么还是我做错了什么?
任何帮助,将不胜感激
回答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 registerNib
in 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
回答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)