我有一个奇怪的问题。我试图以编程方式将dataSource分配给表。
我已UITableView
使用界面生成器在ViewController中为其创建了一个和IBOutlet。我创建了一个实现的类UITableViewDataSource
。我将dataSource
表的设置为dataSource的实例。一切都会编译并正常运行,直到设置dataSource的行在运行时执行。
错误是Thread 1: EXC_BAD_ACCESS (code=EXC_i386_GPFLT)
并且class AppDelegate
定义线突出显示。
class ViewController: UIViewController {
@IBOutlet weak var table: UITableView!
override func viewDidLoad() {
let ds = MyData()
table.dataSource = ds // <---- Runtime error
table.reloadData()
super.viewDidLoad()
}
// ... other methods
}
class MyData: NSObject, UITableViewDataSource {
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = UITableViewCell()
cell.textLabel.text = "a row"
return cell
}
}
有什么想法为什么我得到这个运行时错误?我正在Swift中使用XCode 6 beta 4。
将您的代码更改为:
class ViewController: UIViewController
{
@IBOutlet weak var table: UITableView!
var dataSource: MyData?
override func viewDidLoad()
{
super.viewDidLoad()
dataSource = MyData()
table.dataSource = dataSource!
}
}
您的应用程序中断,因为返回时会ds
立即将其释放viewDidLoad
。您必须保留对数据源的引用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句