Swift WatchKit Tables在打开可选项时找到nil

时间:2018-03-08 16:45:08

标签: ios swift watchkit swift4 apple-watch

我已经看到一堆线程报告此错误,但是,在尝试所有解决方案后,我仍然遇到同样的问题。使用调试器运行我可以看到.setNumberOfRows方法有效,因为它为我的表分配了适当的行数。

import WatchKit

import Foundation


class CountInterfaceController: WKInterfaceController {

//MARK: Properties
@IBOutlet var dataTable: WKInterfaceTable!

var counters = [Counter]()





override func awake(withContext context: Any?) {
    super.awake(withContext: context)

    // Configure interface objects here.

    if let savedCounters = loadCounters(){
        counters+=savedCounters
    }else{
        loadSampleCounters()
    }

    dataTable.setNumberOfRows(counters.count, withRowType: "CountRowController")

    for(index, counter) in counters.enumerated(){

        let row = dataTable.rowController(at: index) as! CountRowController
            row.countNameLabel.setText(counter.name)
            row.countCounterLabel.setText("\(counter.count)")

    }
}

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()
}


private func loadSampleCounters(){

    guard let counter1 = Counter(name:"Pots of coffee", count: 20) else {
        fatalError("Unable to instantiate counter 1")
    }

    guard let counter2 = Counter(name:"Tea kettles", count: 5) else {
        fatalError("Unable to instantiate counter 2")
    }

    counters += [counter1, counter2]
}

我创建了一个表行类,并将其添加到表行控制器的标识字段中,如下图所示。该类包含表格行中两个标签的出口。

Table Row Controller with class

我尝试使用简单的字符串数组中的随机数据填充表格,但这也不起作用(相同的错误)。老实说,我没有看到我在这里失踪的东西......我唯一能想到的是Swift 4中的一些变化,因为大部分的答案和信息都是我所做的。发现与Swift 3有关。

error

0 个答案:

没有答案