ios - Tableview 未加载/触发

标签 ios xcode uitableview swift

我有以下单 View 应用程序。 tablview 委托(delegate)/数据源通过 IB 连接到 Root View Controller 。当应用程序加载时,tableview 是空的。我在 ViewController.swift 文件的所有方法中都设置了一个断点,但没有命中。

enter image description here

我在 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 应用程序文件:

enter image description here

-- 编辑--

单元格标识符已更新,但表中仍然没有数据。

enter image description here

下面是从 tableveiw 到 Root View Controller 的连接。

enter image description here

最佳答案

看起来你有两个问题:

1。 IB 中未正确分配 ViewController 类

选择 Root View Controller 后,右侧 Pane 中会出现什么?它应该是 ViewController(您的 ViewController 类的名称),如下所示:

enter image description here

请注意,IB 不会让您选择不是 UITableViewController 子类的类。如果你有一个空的 Storyboard 并拖出一个导航 Controller ,它创建的 Root View Controller 是一个 TableView Controller ,它只会让你将其与 UITableViewController 子类相关联。如果您真的不想使用 UITableViewController,您必须删除 IB 为您创建的 Root View Controller,拖出一个常规的 View Controller,添加到 Table View 中,然后将所有内容连接起来需要手动。

2。 socket 未正确接线

您连接 socket 的方式看起来不对。 UITableView 应该有引用 RootViewController 的导出 dataSourcedelegate。像这样:

enter image description here

而 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/

相关文章:

ios - 无法从 sqlite 中删除我的 TableView 中的行数据

ios - 将数据写入 HTML 模板

ios - 点击 MKAnnotationView 不会调用 didSelectAnnotationView 委托(delegate)

iphone - Objective-C初学者(发布)问题

xcode - 如何修复 "file can' 无法打开,因为它来自身份不明的开发人员”错误?

ios - 如何将数据从一个表格 View 单元格传输到另一个表格 View 单元格?

ios - 核心数据 : difference between backgroundContext and child context with privateQueueConcurrencyType?

objective-c - 如何使CGContextShowTextAtPoint()自动缩小某个矩形中的文本

ios - Swift 中的链接器命令失败,退出代码为 1(使用 -v 查看调用)

ios - 为什么 UIPageViewController 中 UITableView 的内容插入在交互后立即变得困惑?