如何在 SwiftUI 中使用列表
How to use a List in SwiftUI
我有一个 WorkoutView,错误发生在 'List(workouts, id: .id)'
错误是 "Type '_' has no member 'id'"
这是视图:
struct WorkoutsView: View {
@State var presentBuilder = false
var workouts = WorkoutData.data()
var body: some View {
NavigationView {
List(workouts, id: \.id) { workout in
NavigationLink(destination: WorkoutDetailView()) {
WorkoutBlockView(name: workout.name, workoutLength: workout.length, amountCompleted: workout.completed())
}
}
.navigationBarItems(leading:
Button(action: {
self.presentBuilder.toggle()
}, label: {
ZStack {
Circle()
.foregroundColor(.blue)
.frame(width: 30, height: 30)
.shadow(radius: 7.0)
Image("add")
.foregroundColor(.black)
}
})
)
}.sheet(isPresented: $presentBuilder) {WorkoutBuilderView()}
}
}
这里是 WorkoutData/Workout:
struct WorkoutData {
static func data() -> [Workout] {
return [Workout(length: 17, name: "Example Workout", description: "workout description"), Workout(length: 17, name: "Example Workout", description: "workout description")]
}
}
struct Workout {
let id = UUID()
var progress = 1
var length: Int
var name: String
var description: String
func completed() -> Int {
return progress - 1
}
如果我没理解错的话,List 无法确定锻炼的类型,因此找不到 id。也许我需要让 Workout 符合 Identifiable(试过这个)?谢谢!
我正在研究类似的 SwiftUI 原型。我使用的是 HStack
,但我认为您可以通过枚举 class Hashable
来做到这一点。
struct Statistic: Hashable {
let title, stat: String
}
// ...
let boxes: [Statistic]
// ...
ScrollView(.horizontal, showsIndicators: false) {
HStack {
ForEach(boxes, id: \.self) { box in
BoxView(box: box)
}
}
}
在你的情况下,我认为你会让 Workout
可散列
struct Workout: Hashable {
let id = UUID()
var progress = 1
var length: Int
var name: String
var description: String
func completed() -> Int {
return progress - 1
}
}
并使用 self
而不是 id
List(workouts, id: \.self) { workout in
与您的(“.id”)无关。这样的List
错误是因为ViewBuilder里面的一些View不正确。
如果我替换
1. WorkoutDetailView())
2. WorkoutBlockView(name: workout.name, workoutLength: workout.length, amountCompleted: workout.completed())
3. WorkoutBuilderView()
和 Text
,左侧的 WorkoutsView 布局是可行的。所以,你得弄清楚为什么那些视图会出现一些问题。
struct WorkoutsView: View {
@State var presentBuilder = false
var workouts = WorkoutData.data()
var body: some View {
NavigationView {
List(workouts, id: \.id) { workout in
NavigationLink(destination: Text("placeHolder")) {
Text("placeHolder")
}
}
.navigationBarItems(leading:
Button(action: {
self.presentBuilder.toggle()
}, label: {
ZStack {
Circle()
.foregroundColor(.blue)
.frame(width: 30, height: 30)
.shadow(radius: 7.0)
Image("add")
.foregroundColor(.black)
}
})
)
}.sheet(isPresented: $presentBuilder) {Text("placeHolder")}
}
}
希望对您有所帮助。
我有一个 WorkoutView,错误发生在 'List(workouts, id: .id)' 错误是 "Type '_' has no member 'id'" 这是视图:
struct WorkoutsView: View {
@State var presentBuilder = false
var workouts = WorkoutData.data()
var body: some View {
NavigationView {
List(workouts, id: \.id) { workout in
NavigationLink(destination: WorkoutDetailView()) {
WorkoutBlockView(name: workout.name, workoutLength: workout.length, amountCompleted: workout.completed())
}
}
.navigationBarItems(leading:
Button(action: {
self.presentBuilder.toggle()
}, label: {
ZStack {
Circle()
.foregroundColor(.blue)
.frame(width: 30, height: 30)
.shadow(radius: 7.0)
Image("add")
.foregroundColor(.black)
}
})
)
}.sheet(isPresented: $presentBuilder) {WorkoutBuilderView()}
}
}
这里是 WorkoutData/Workout:
struct WorkoutData {
static func data() -> [Workout] {
return [Workout(length: 17, name: "Example Workout", description: "workout description"), Workout(length: 17, name: "Example Workout", description: "workout description")]
}
}
struct Workout {
let id = UUID()
var progress = 1
var length: Int
var name: String
var description: String
func completed() -> Int {
return progress - 1
}
如果我没理解错的话,List 无法确定锻炼的类型,因此找不到 id。也许我需要让 Workout 符合 Identifiable(试过这个)?谢谢!
我正在研究类似的 SwiftUI 原型。我使用的是 HStack
,但我认为您可以通过枚举 class Hashable
来做到这一点。
struct Statistic: Hashable {
let title, stat: String
}
// ...
let boxes: [Statistic]
// ...
ScrollView(.horizontal, showsIndicators: false) {
HStack {
ForEach(boxes, id: \.self) { box in
BoxView(box: box)
}
}
}
在你的情况下,我认为你会让 Workout
可散列
struct Workout: Hashable {
let id = UUID()
var progress = 1
var length: Int
var name: String
var description: String
func completed() -> Int {
return progress - 1
}
}
并使用 self
而不是 id
List(workouts, id: \.self) { workout in
与您的(“.id”)无关。这样的List
错误是因为ViewBuilder里面的一些View不正确。
如果我替换
1. WorkoutDetailView())
2. WorkoutBlockView(name: workout.name, workoutLength: workout.length, amountCompleted: workout.completed())
3. WorkoutBuilderView()
和 Text
,左侧的 WorkoutsView 布局是可行的。所以,你得弄清楚为什么那些视图会出现一些问题。
struct WorkoutsView: View {
@State var presentBuilder = false
var workouts = WorkoutData.data()
var body: some View {
NavigationView {
List(workouts, id: \.id) { workout in
NavigationLink(destination: Text("placeHolder")) {
Text("placeHolder")
}
}
.navigationBarItems(leading:
Button(action: {
self.presentBuilder.toggle()
}, label: {
ZStack {
Circle()
.foregroundColor(.blue)
.frame(width: 30, height: 30)
.shadow(radius: 7.0)
Image("add")
.foregroundColor(.black)
}
})
)
}.sheet(isPresented: $presentBuilder) {Text("placeHolder")}
}
}
希望对您有所帮助。