【发布时间】:2021-10-06 23:12:39
【问题描述】:
我有这个模型,我在一个 tableview 上使用并填充它:
struct TestSection {
var title: String
var options: [TestOption]
}
struct TestOption {
var field: String
var placeholder: String
}
var models = [TestSection]()
override func viewDidLoad() {
super.viewDidLoad()
models.append(TestSection(title: "Test", options: [
TestOption(placeholder: "Test"),
TestOption(placeholder: "Test 2"),
TestOption(placeholder: "Test 3")
]))
}
使用委托传回一些数据后,我可以更改特定索引处的模型值,如下所示: 模型[0].options[1].placeholder = value
我想用枚举实现同样的目标。我的带有枚举的模型如下所示:
struct TestSection {
var title: String
var options: [TestOptionType]
}
enum TestOptionType {
case testOneCell(model: TestOptionOne)
case testTwoCell(model: TestOptionTwo)
case testThreeCell(model: TestOptionThree)
}
struct TestOptionOne {
var field: String
var placeholder: String
}
struct TestOptionTwo {
var tag: Int
var placeholder: String
var handler: (() -> Void)
}
struct TestOptionThree {
var firstName: String
var lastName: String
var handler: (() -> Void)
}
models.append(TestSection(title: "Description", options: [
.testOneCell(model: TestOptionOne(field: "Test", placeholder: "Test")),
.testTwoCell(model: TestOptionTwo(tag: 1, placeholder: "Test", handler: {
///handle handler
})),
.testThreeCell(model: TestOptionThree(firstName: "Test", lastName: "Test", handler: {
///handle handler
}))
]))
如何使用 models[0].options[0] 更改我的结构之一的值。当我尝试它时,我只能将 'self' 添加到诸如 'models[0].options[0].self' 这样的选项的末尾,但这不是我想要的。
【问题讨论】:
-
如果你写了一些代码来改变
options[0].field,因为你认为options[0]是testOneCell的情况,但是当那行被执行时,options[0]是实际上testTwoCell,它没有field属性? -
你需要的是
models[0].options[0] = .testOneCell(model: .init(field: "", placeholder: ""))
标签: ios swift uitableview enums