ios - 是否可以在详细视图中编辑 SwiftUI 列表元素?
问题描述
我想在 SwiftUI 的 NavigationView 的详细视图中编辑列表元素的属性。如果我在同一个视图中编辑元素的属性,列表会自动更新。如果我打开列表元素的详细视图并编辑属性,它会正确存储在元素中并且也可以在主视图中访问,但列表不会更新。这是代码的一部分:
@ObservedObject var actions: ActionsList = ActionsList(l:[Action(dur: 1),Action(dur: 5), Action(dur: 10) ])
NavigationView{
List(actions.list, id: \.id ){ action in
NavigationLink(destination: ActionDetail(action:action)){
Timer_cell( action: action)
}
}
}
动作细节:
struct ActionDetail: View {
@State var action: Action
var body: some View {
Form {
HStack {
Text("Dauer")
Spacer()
TextField("Dauer", value: $action.duration, formatter: self.numberFormatter)
}
}
}
var numberFormatter : Formatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.generatesDecimalNumbers = true
return formatter
}()
}
动作列表:
import Foundation
class ActionsList: ObservableObject {
@Published var list :Array<Action> = [];
init(l : Array<Action>) {
list = l;
}
init(){
list = []
}
}
行动:
struct Action : Identifiable, Equatable, Hashable{
static func == (lhs: Action, rhs: Action) -> Bool {
lhs.duration == rhs.duration && lhs.type == rhs.type && lhs.id == rhs.id
}
var active: Bool
var duration: Int //in seconds
var type: ActionType
var id: UUID
init() {
active = true
duration = 10
type = ActionType.active
id = UUID()
}
init(dur: Int){
active = true
duration = dur
type = ActionType.active
id = UUID()
}
init(_id: UUID, _type: ActionType, dur: Int, _active: Bool){
id = _id
type = _type
duration = dur
active = _active
}
func hash(into hasher: inout Hasher) {
hasher.combine(id)
hasher.combine(duration)
hasher.combine(type)
hasher.combine(active)
}
}
enum ActionType {
case pause, active, warmup, cooldown
}
解决方案
首先,您必须将操作列表更改为环境变量,如下所示
@EnvironmentObject var actions: ActionsList
你可以从下面的代码中调用 ListView
let contentView = ActionListView().environmentObject(ActionsList(l:[Action(dur: 1),Action(dur: 5), Action(dur: 10) ]))
导航到详细信息视图时
NavigationView{
List(actions.list.indices){ index in
NavigationLink(destination: ActionDetail().environmentObject(self.actions.list[index])){
Timer_cell( action: self.$actions.list[index]) // this line does the trick
}
}
}
将您的 Action 模型更改struct
为Class
并符合Observable
协议
class Action : Identifiable, Equatable, Hashable, ObservableObject {
static func == (lhs: Action, rhs: Action) -> Bool {
lhs.duration == rhs.duration && lhs.type == rhs.type && lhs.id == rhs.id
}
var active: Bool
@Published var duration: Int
}
希望它会帮助你。
推荐阅读
- php - 有没有办法从 PHP 生成与 nodejs 相同的 Hmac 哈希输出?
- xsd - XSD 中的空模型组定义是否会更改定义的有效 XML 语言?
- sql - 选择列 X 的总和大于或等于 Y 的所有行
- assembly - 这个汇编代码中 r10 和 r20 的地址是什么?
- android - Android 布局:线性布局的高度不够
- android - 移动滚动视图而不是单击 Kotlin 中的选项卡时更改选项卡位置
- django - Django Utils 6 for Django 3.1.5
- python - 使用 python 在 REDIS 中处理特殊 JSON 字符
- c++ - 如何正确使用合并功能?
- python - 为什么 PyAutoGui 命令在 cmd 中不起作用?