我有以下单 View 应用程序。 tablview 委托(delegate)/数据源通过 IB 连接到 Root View Controller 。当应用程序加载时,tableview 是空的。我在 ViewController.swift 文件的所有方法中都设置了一个断点,但没有命中。
我在 ViewController.swift 中添加了以下内容(UITableViewDataSource 现在也在那里):
UITableViewDelegate
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "test")
cell.textLabel?.text = "just a cell"
return cell;
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
由于在 ViewController.swift 中没有遇到断点,我认为仍然没有连接。知道这可能是什么或我做错了什么吗?
该项目只有库存的单一 View 应用程序文件:
-- 编辑--
单元格标识符已更新,但表中仍然没有数据。
下面是从 tableveiw 到 Root View Controller 的连接。
最佳答案
看起来你有两个问题:
1。 IB 中未正确分配 ViewController 类
选择 Root View Controller 后,右侧 Pane 中会出现什么?它应该是 ViewController(您的 ViewController 类的名称),如下所示:
请注意,IB 不会让您选择不是 UITableViewController
子类的类。如果你有一个空的 Storyboard 并拖出一个导航 Controller ,它创建的 Root View Controller 是一个 TableView Controller ,它只会让你将其与 UITableViewController
子类相关联。如果您真的不想使用 UITableViewController
,您必须删除 IB 为您创建的 Root View Controller,拖出一个常规的 View Controller,添加到 Table View 中,然后将所有内容连接起来需要手动。
2。 socket 未正确接线
您连接 socket 的方式看起来不对。 UITableView
应该有引用 RootViewController 的导出 dataSource
和 delegate
。像这样:
而 RootViewContoller 应该有引用 导出数据源和引用 TableView 的委托(delegate)。
从您得到的崩溃 ([UINavigationItem tableView:numberOfRowsInSection:]: unrecognized selector sent to instance
) 很明显您已经连接了数据源和委托(delegate)给导航栏,而不是 View Controller (黄色圆圈 Storyboard 层次结构 View )。
关于ios - Tableview 未加载/触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29035779/