swift - 如何将 UITableView 作为 subview 添加到 xib 中的 UIView?

标签 swift uitableview uiview xib subview

我有一个使用 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 上看到绿色背景,但表格单元格没有显示

no table cells! 我知道表格的导出不是零,因为在调试时,如果我使用控制台下方的 Quick Look 图标检查 table 变量,我可以看到表格。

更新

我意识到我的方法不是一个好方法。

我通过使我的 View 成为 UITableViewDataSourceUITableViewDelegate 的 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/

相关文章:

swift - dispatch_semaphore_signal 不会调用 PHImageManager requestImageDataForAsset

ios - 当从 UISplitViewController 显示时,UITableViewController 自动滚动停止考虑键盘

ios - 在大型 UIView 上绘制占用大量内存

ios - UIScrollView 不滚动的自定义 UIView 小部件

ios - 如何在 iOS 中动态地在 UIView 中制作不同的圆角值?

ios - 位置权限提醒按钮回调

ios - 打开和关闭特定 viewController 的声音

ios - Sync 解决 Terminating app due to uncaught exception 'NSGenericException',原因 : '*** Collection <__NSCFSet: > was mutated while being enumerated

ios - Objective-C - iPhone : different DetailViewControllers for TableView?

ios - 删除 TableView 行会引发线程 1 : signal SIGABRT