我有一个使用 xib
文件构建的自定义 View 。在此自定义 View 中,有一个 UIView
,我想在其中添加一个静态 UITableView
作为 subview 。此处显示的 UITableView
出现在 storyboard
上并关联到 UITableViewController
。我做了以下事情:
let vc = (UIStoryboard(name: "Main", bundle: nil))
.instantiateViewControllerWithIdentifier("tableController") as! TableController
vc.loadViewIfNeeded()
table = vc.table // this is an outlet, I'm sure it is not nil
table.delegate = vc
table.dataSource = vc
table.backgroundColor = UIColor.greenColor()
containerView.addSubview(table)
我可以在我的自定义 View 上看到绿色背景,但表格单元格没有显示
我知道表格的导出不是零,因为在调试时,如果我使用控制台下方的 Quick Look
图标检查 table
变量,我可以看到表格。
更新
我意识到我的方法不是一个好方法。
我通过使我的 View 成为 UITableViewDataSource
和 UITableViewDelegate
的 subview 并以编程方式创建 UITable
和所需的 来实现我想要的结果>TableViewCell
。大量的代码!!
最佳答案
The UITableView to be shown here is present on the storyboard and associated to a UITableViewController.
在那种情况下,您所做的是完全非法的,这就是它不起作用的原因。您不能只调用 instantiateViewControllerWithIdentifier
,获取 View Controller 的 View ,然后使用 addSubview
将 View 填充到界面中。
毕竟,想一想:如果您这样做, View Controller 会发生什么?它不存在了,你得到了一个完全没有功能的 View 。
为了使 TableController 成为 self
(当前 View Controller - 而您不是跳舞。
关于swift - 如何将 UITableView 作为 subview 添加到 xib 中的 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41813208/