insertRowAtIndexes watchKit迅捷

大卫·罗伯逊

我有一个结构

struct Question {
    var title: [String]
    var additionalInfo: String?
    var answers: [String]
}

和我向其中添加数据的变量

var questions = [
    Question(title: ["What is the color of?", "additional color information"], additionalInfo: nil, answers: [
        "Blue",
        "Gray"
        ])
]

此数据将加载tableView到AppleWatch上。两种行类型是分开的- TitleRowType(对于标题数组)和AnswersRowType(对于答案数组)。

当我将值插入struct's数组时-我想tableView用动画插入其中的行

我知道有一个insertRowAtIndexes功能,但是我无法将其包裹住。Apple文档中提供的示例对我不起作用。那就是我想出的:

    let indexSet = NSIndexSet(index: Int) // Int is passed via the function
    tableView.insertRowsAtIndexes(indexSet, withRowType: "TitleRowType")

但是当我运行它时-表格不会更新。期待您的建议。

琼恩

您必须执行3个步骤:

  1. 将新数据添加到您的阵列
  2. 在表格中插入一行
  3. 用新数据填充行

这是一个简单的示例:

class InterfaceController: WKInterfaceController {

    @IBOutlet var table: WKInterfaceTable!
    var items = ["row1", "row2", "row3"]

    override func awakeWithContext(context: AnyObject?) {
        super.awakeWithContext(context)
        loadTable()
    }

    func loadTable() {
        table.setNumberOfRows(items.count, withRowType: "tableRow")
        var rowIndex = 0
        for item in items {
            if let row = table.rowControllerAtIndex(rowIndex) as? TableRowController {
                row.label.setText(item)
            }
            rowIndex++
        }
    }

    @IBAction func insertRow() {
        items.append("row4")
        let newIndex = items.count
        table.insertRowsAtIndexes(NSIndexSet(index: newIndex), withRowType: "tableRow")
        if let row = table.rowControllerAtIndex(newIndex) as? TableRowController {
            row.label.setText(items[newIndex])
        }
    }
}

TableRowControllerNSObject具有一个WKInterfaceLabel出口以显示行号的子类

我用一个按钮触发 insertRow()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章