我有一个结构
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个步骤:
这是一个简单的示例:
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()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句