gpt4 book ai didi

ios - insertRowAtIndexes watchKit swift

转载 作者:搜寻专家 更新时间:2023-11-01 07:29:54 24 4
gpt4 key购买 nike

我有一个结构

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"
])
]

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

当我将值插入到 struct 的 数组中时 - 我希望 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])
}
}
}

TableRowController 是一个 NSObject 子类,它有一个 WKInterfaceLabel 导出来显示行数。

我用了一个按钮来触发 insertRow()

关于ios - insertRowAtIndexes watchKit swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33277265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com