ios - WKInterfaceTable 和 setNumberOfRows 崩溃 "unexpectedly found nil while unwrapping an Optional value"

标签 ios swift watchkit wkinterfacetable wkinterfacecontroller

我正在使用 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/

相关文章:

iphone - xcode 从 svn 更改为 git

ios - Watch Connectivity 第一次工作但之后就不行了

ios - 如何将一些消息或字符串从 iphone 设备传递到配对的 Apple Watch

ios - 将 Firebase 数据从主视图 Controller 发送到详细 View Controller

ios - 无法在 OS X El Capitan 中使用命令 genstrings

ios - 如何修复 Xcode : Key Commands must all have a Title, 键和选择器中的以下错误?

ios - 如何对 AVPlayer 的播放和暂停操作执行一些操作?

swift - Realm swift : Handling/preventing sync error 108

ios - WatchKit App 作为 iOS 设备的 Controller

ios - Swift:触发 TableViewCell 以导致另一个 ViewController 中的 UIWebView 中的链接