Created
February 28, 2020 15:38
-
-
Save FarshidRoohi/a660556888489cd1e486de3246cbee7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct ContentView: View { | |
@ObservedObject var myViewModel = MyViewModel() | |
var body: some View { | |
ScrollView { | |
VStack { | |
ForEach(self.myViewModel.items.indices , id : \.self) {index in | |
RowForTest(item: self.myViewModel.items[index]) | |
} | |
Button(action: { | |
print(self.myViewModel.items[0].value) | |
}){ | |
Text("Submit") | |
}.padding() | |
} | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} | |
struct RowForTest: View { | |
@State var item :MyModel | |
var body: some View { | |
HStack { | |
Text(item.title) | |
TextField("enter value", text: Binding(get: { | |
return self.item.value | |
}, set: { newValue in | |
self.item.value = newValue | |
})) | |
}.padding() | |
} | |
} |
Thank you very much for your guidance
I learned :))
Really helpful to start with
But what if I have multiple textfields(name, value, oneLinedescription) in each row of list? Should I create 3 variables in RowForTest for write or is there any better way to pass entire object for write?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
MyViewModel.swift
RowForTest.swift
ContentView
's ForEach as:The whole code: