How can I get the value of the selected row in a list?
For example:
var data: [String] = []
List {
ForEach(0..<data.count, id: .self) { item in
HStack {
Text(data[item])
}
}
} //: End of the List
.onDelete(perform: { indexSet in
// get the value of the row
// get the corresponding key for the value
// delete the value by the key from the dictionary
}) //: End of the onDelete
.onAppear {
let dictionary = //retrieve the data from the database and get a dictionary
for entry in dictionary {
data.append(entry.value)
}
} //: End of the onAppear
How I can get the value of a selected row of the list?
2
Answers
Use
List
with selection constructor, likeIf getting value of the deleted element is a must, then you can try it this way. Code is below the image.
If you only need the value of selected element, then just use
$selection
.