我试图创建一个在第一列之后水平滚动的表,我对每一列都有一个for循环,以便从数组中获取数据。我知道这不是正确的方法,因为我要循环同一个数组多少次,但我想不出一个更好的解决方案。
struct SampleData: Identifiable {
let id = UUID()
let Entity: String
let address1: String
let address2: String
let city: String
let state: String
let zip: Int
let website: String
let billToName: String
let billable: Bool
let hours: String
let accountNo: Int
let BillToEntity: String
let email: String
}
let datas = [
SampleData(
Entity: "Entity 1", address1: "1234 N. Main", address2: "Suite 200", city: "austin",
state: "TX", zip: 12345, website: "www.website.com", billToName: "Test Name 1", billable: false,
hours: "8-5 M-F", accountNo: 123_456_789, BillToEntity: "Bill To Entity 1",
email: "test@test.com"),
SampleData(
Entity: "Entity 2", address1: "5678 N. Main", address2: "Suite 300", city: "livingston",
state: "TX", zip: 12345, website: "www.website.com", billToName: "Test Name 2", billable: false,
hours: "8-5 M-F", accountNo: 123_456_789, BillToEntity: "Bill To Entity 2",
email: "test@test.com"),
SampleData(
Entity: "Entity 3", address1: "90025 N. Main", address2: "Suite 400", city: "houston",
state: "TX", zip: 12345, website: "www.website.com", billToName: "Test Name 3", billable: true,
hours: "8-5 M-F", accountNo: 123_456_789, BillToEntity: "Bill To Entity 3",
email: "test@test.com"),
SampleData(
Entity: "Entity 4", address1: "4456 N. Main", address2: "Suite 500", city: "spring",
state: "TX", zip: 12345, website: "www.website.com", billToName: "Test Name 4", billable: true,
hours: "8-5 M-F", accountNo: 123_456_789, BillToEntity: "Bill To Entity 4",
email: "test@test.com"),
SampleData(
Entity: "Entity 5", address1: "4456 N. Main", address2: "Suite 500", city: "spring",
state: "TX", zip: 12345, website: "www.website.com", billToName: "Test Name 4", billable: true,
hours: "8-5 M-F", accountNo: 123_456_789, BillToEntity: "Bill To Entity 4",
email: "test@test.com"),
SampleData(
Entity: "Entity 6", address1: "56 N. Main", address2: "Suite 500", city: "spring", state: "TX",
zip: 12345, website: "www.website.com", billToName: "Test Name 4", billable: false,
hours: "8-5 M-F", accountNo: 123_456_789, BillToEntity: "Bill To Entity 4",
email: "test@test.com"),
SampleData(
Entity: "Entity 7", address1: "4456 N", address2: "Suite 500", city: "spring", state: "TX",
zip: 12345, website: "www.website.com", billToName: "Test Name 4", billable: true,
hours: "8-5 M-F", accountNo: 123_456_789, BillToEntity: "Bill To Entity 4",
email: "test@test.com"),
SampleData(
Entity: "Entity 8", address1: "44 Main", address2: "Suite 500", city: "spring", state: "TX",
zip: 12345, website: "www.website.com", billToName: "Test Name 4", billable: true,
hours: "8-5 M-F", accountNo: 123_456_789, BillToEntity: "Bill To Entity 4",
email: "test@test.com"),
]
struct TablesView: View {
@State var billable = false
var body: some View {
HStack (alignment: .top){
VStack {
Text("Entity")
HStack(spacing: 16) {
VStack {
ForEach(datas) { val in
Text(val.Entity)
}
}
}
}
ScrollView(.horizontal) {
HStack(alignment: .top) {
VStack {
VStack {
Text("Address1")
ForEach(datas) { val in
Text(val.address1)
}
}
}
VStack {
VStack {
Text("Address2")
ForEach(datas) { val in
Text(val.address2)
}
}
}
VStack {
VStack {
Text("City")
ForEach(datas) { val in
Text(val.city)
}
}
}
VStack {
VStack {
Text("State")
ForEach(datas) { val in
Text(val.state)
}
}
}
VStack {
VStack {
Text("Zip")
ForEach(datas) { val in
Text(String(val.zip))
}
}
}
VStack {
VStack {
Text("Website")
ForEach(datas) { val in
Text(val.website)
}
}
}
VStack {
VStack {
Text("Bill To")
ForEach(datas) { val in
Text(val.billToName)
}
}
}
VStack {
VStack {
Text("Billable")
ForEach(datas) { val in
Image(systemName: val.billable ? "checkmark.square.fill" : "square")
.foregroundColor(val.billable ? Color(UIColor.systemBlue) : Color.secondary)
.onTapGesture {
self.billable.toggle()
}
}
}
}
}
}
}
}
}
发布于 2022-12-03 04:03:29
使用Grid
可以使这种情况变得更好。这里没有把整件事都打出来,但你知道
ScrollView(.horizontal) {
Grid(alignment: .leading) {
GridRow {
Text("Address 1")
Text("Address 2")
Text("city")
}
ForEach(datas) { data in
GridRow {
Text(data.address1)
Text(data.address2)
Text(data.city)
}
}
}
}
发布于 2022-12-03 19:08:56
SwiftUI已经有了一个内置的Table
。
var body: some View {
Table(datas) {
TableColumn("Address 1", value: \.address1)
TableColumn("Address 2", value: \.address2)
TableColumn("City", value: \.city)
}
}
https://stackoverflow.com/questions/74663827
复制相似问题