我正在使用 WatchKit,我有一个只有一个表的简单界面,但我只用了几行代码就出错了,也许我忘记了一些非常基本的东西。
我的界面:
表格内的行具有标识符:
和自定义类:
Controller 由这段代码实现:
import WatchKit
import Foundation
class ActiveListController: WKInterfaceController
{
@IBOutlet weak var tableView: WKInterfaceTable!
override func awakeWithContext(context: AnyObject?)
{
super.awakeWithContext(context)
loadData()
}
override func willActivate()
{
// This method is called when watch view controller is about to be visible to user
super.willActivate()
}
override func didDeactivate()
{
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
func loadData()
{
tableView.setNumberOfRows(10, withRowType: "ItemRow") // GET ERROR
for index in 0...9
{
let row = tableView.rowControllerAtIndex(index) as! ItemRow
row.nameLabel.setText("test")
}
}
}
显然我有我的单行自定义类
import Foundation
import WatchKit
class ItemRow : NSObject
{
@IBOutlet weak var checkImage: WKInterfaceImage!
@IBOutlet weak var nameLabel: WKInterfaceLabel!
}
因此,当我运行该应用程序时,当我尝试设置行数时出现错误,但实际上我无法理解什么是 nil:
fatal error: unexpectedly found nil while unwrapping an Optional value
也许这是一个简单的错误,也许不是,但请帮助我:\
最佳答案
代码中的 for
循环从 1...10
开始,它应该是 0...9
因为行是基于0的
关于ios - WKInterfaceTable 和 setNumberOfRows 崩溃 "unexpectedly found nil while unwrapping an Optional value",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30799833/