【发布时间】:2015-05-04 20:15:43
【问题描述】:
我在使用 Swift 中的 UITableView 时遇到问题。 只要我不实现处理单元格选择的委托函数,它就可以正常工作。
这是我非常简单的类,它充当表视图委托和数据源:
import UIKit
class TableViewService : NSObject, UITableViewDataSource, UITableViewDelegate {
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
if(indexPath.row == 0) {
cell.textLabel?.text = "One"
} else {
cell.textLabel?.text = "Two"
}
cell.selectionStyle = .None
return cell;
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
log.debug("Row selceted")
}
}
只要我在表视图中选择一行,应用程序就会崩溃,并在 objc_msgSend 中出现 EXEC_BAD_ACCESS 错误 - 控制台中没有记录任何错误,如果我在 didSelectRowAtIndexPath 函数上设置断点,它永远不会被触发。
任何想法我错过了什么或做错了什么?
【问题讨论】:
-
想显示视图控制器的代码,你的 tableview 是在哪里实现的?
-
什么是
log,你为那个类初始化了吗? -
@RoryMcKinnel - 日志实例是一个全局 XCLogger。但在这种情况下它不相关。我在 didSelectRowAtIndexPath 函数中输入了什么代码并不重要 - 它仍然会崩溃。
-
@AndreSlotta - 我的 ViewController 基本上只加载一个包含表格视图的 NIB 文件。我已将委托和数据源设置为 NIB 文件中的对象。也许最好从视图控制器手动设置它们。
标签: ios uitableview swift