英文:
How to obtain cell value of selected row in SwiftUI Table
问题
我现在是你的中文翻译,这是你要的翻译内容:
import SwiftUI
struct Entrant: Identifiable {
public let id = UUID()
public var Flags: String
public var Name: String
public var Rank: String
public var Obs: String
public var Pl: String
public var Idle: String
}
private var entrantList = [
Entrant(Flags: "a",
Name: "Chavez",
Rank: "1d",
Obs: "-",
Pl: "1",
Idle: "29m"),
Entrant(Flags: "a",
Name: "Chen",
Rank: "1k",
Obs: "-",
Pl: "1",
Idle: "33m"),
Entrant(Flags: "a",
Name: "Clark",
Rank: "10k",
Obs: "-",
Pl: "-",
Idle: "39m"),
Entrant(Flags: "a",
Name: "Clark",
Rank: "10k",
Obs: "-",
Pl: "-",
Idle: "39m"),
Entrant(Flags: "a",
Name: "zxH3",
Rank: "10k",
Obs: "-",
Pl: "-",
Idle: "39m"),
Entrant(Flags: "a",
Name: "pkls3",
Rank: "10k",
Obs: "-",
Pl: "-",
Idle: "39m"),
Entrant(Flags: "X",
Name: "xanadu6291",
Rank: "5k",
Obs: "-",
Pl: "-",
Idle: "0s"),
]
struct ContentView: View {
@State private var entrants = entrantList
@State private var selection: Entrant.ID? = nil
var body: some View {
VStack {
Table(entrants, selection: $selection) {
TableColumn("Flags", value:\.Flags).width(35)
TableColumn("Name", value:\.Name).width(80)
TableColumn("Rank", value:\.Rank).width(30)
TableColumn("Obs", value:\.Obs).width(30)
TableColumn("Pl", value:\.Pl).width(20)
TableColumn("Idle", value:\.Idle).width(30)
}.frame(width: 350, height: 200)
Button("Push Me!") {
let _ = print(selection!.description)
}
}
}
}
希望这有所帮助。如果你有任何其他问题,请随时提出。
英文:
I'm a SwiftUI Beginner. I'm now trying to obtain cell value of selected row in SwiftUI Table. With Cocoa bindings, it is easy. Just like write as ArrayController.selection.Name
. Or using SwiftUI List, I can obtain selection result like the following:
import SwiftUI
struct ContentView: View {
@State private var listElements = ["First", "Second", "Third", "Forth"]
@State private var selectedOne: String? = nil
var body: some View {
GroupBox("List") {
List(listElements, id:\.self, selection: $selectedOne) { element in
Text(element)
}
}
GroupBox("Selection") {
Text("Selection: \(selectedOne == nil ? "None" : selectedOne!)")
}
.frame(minHeight: 300)
}
}
But I can't find out how to do with SwiftUI Table. Here's my trying Table. Currently I just get uuid of selected row with selection.description
:
import SwiftUI
struct Entrant: Identifiable {
public let id = UUID()
public var Flags: String
public var Name: String
public var Rank: String
public var Obs: String
public var Pl: String
public var Idle: String
}
private var entrantList = [
Entrant(Flags: "a",
Name: "Chavez",
Rank: "1d",
Obs: "-",
Pl: "1",
Idle: "29m"),
Entrant(Flags: "a",
Name: "Chen",
Rank: "1k",
Obs: "-",
Pl: "1",
Idle: "33m"),
Entrant(Flags: "a",
Name: "Clark",
Rank: "10k",
Obs: "-",
Pl: "-",
Idle: "39m"),
Entrant(Flags: "a",
Name: "Clark",
Rank: "10k",
Obs: "-",
Pl: "-",
Idle: "39m"),
Entrant(Flags: "a",
Name: "zxH3",
Rank: "10k",
Obs: "-",
Pl: "-",
Idle: "39m"),
Entrant(Flags: "a",
Name: "pkls3",
Rank: "10k",
Obs: "-",
Pl: "-",
Idle: "39m"),
Entrant(Flags: "X",
Name: "xanadu6291",
Rank: "5k",
Obs: "-",
Pl: "-",
Idle: "0s"),
]
struct ContentView: View {
@State private var entrants = entrantList
@State private var selection: Entrant.ID? = nil
var body: some View {
VStack {
Table(entrants, selection: $selection) {
TableColumn("Flags",value:\.Flags).width(35)
TableColumn("Name", value:\.Name).width(80)
TableColumn("Rank", value:\.Rank).width(30)
TableColumn("Obs", value:\.Obs).width(30)
TableColumn("Pl", value:\.Pl).width(20)
TableColumn("Idle", value:\.Idle).width(30)
}.frame(width: 350, height: 200)
Button("Push Me!") {
let _ = print(selection!.description)
}
}
}
}
Any help is appreciable.
答案1
得分: 1
使用onChange来观察selection值的更改。
struct ContentView: View {
@State private var entrants = entrantList // 您的项目列表
@State private var selection: UUID? = nil
var body: some View {
VStack {
Table(entrants, selection: $selection) {
TableColumn("Flags", value: \.Flags).width(35)
TableColumn("Name", value: \.Name).width(80)
TableColumn("Rank", value: \.Rank).width(30)
TableColumn("Obs", value: \.Obs).width(30)
TableColumn("Pl", value: \.Pl).width(20)
TableColumn("Idle", value: \.Idle).width(30)
}
.onChange(of: selection, perform: { selected in
// 在表格选择更改时每次调用
let item = entrants.first(where: { $0.id == selected })
print("Selected id: ", selected!)
print("Selected item: ", item as Any)
})
.frame(width: 350, height: 200)
}
}
}
[1]: https://developer.apple.com/documentation/swiftui/view/onchange(of:perform:)
英文:
Use onChange to observe selection value changes.
struct ContentView: View {
@State private var entrants = entrantList // your list of items
@State private var selection: UUID? = nil
var body: some View {
VStack {
Table(entrants, selection: $selection) {
TableColumn("Flags",value:\.Flags).width(35)
TableColumn("Name", value:\.Name).width(80)
TableColumn("Rank", value:\.Rank).width(30)
TableColumn("Obs", value:\.Obs).width(30)
TableColumn("Pl", value:\.Pl).width(20)
TableColumn("Idle", value:\.Idle).width(30)
}
.onChange(of: selection, perform: { selected in
// called each time on table selection changes
let item = entrants.first(where: { $0.id == selected })
print("Selected id : ", selected!)
print("Selected item : ", item as Any)
})
.frame(width: 350, height: 200)
}
}
}
答案2
得分: 1
你可以尝试在你的按钮中添加以下内容,以获取选定行的值:
Button("Push Me!") {
let _ = print(selection!.description)
if let item = entrants.first(where: {$0.id == selection}) {
print("---> item: \(item)")
}
}
英文:
You could try adding this in your button, to get ...value of selected row...
:
Button("Push Me!") {
let _ = print(selection!.description)
if let item = entrants.first(where: {$0.id == selection}) {
print("---> item: \(item)")
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论