简体   繁体   中英

How to use a List in SwiftUI

I have a WorkoutView, and the error occurs at 'List(workouts, id: .id)' The error is "Type '_' has no member 'id'" Here is the View:

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()}

}

}

And here is the 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
}

If I understand correctly, the List can't figure out the type of workouts, and therefore can't find id. Maybe I need to make Workout conform to Identifiable (tried this)? Thank you!

I working on something similar SwiftUI prototype. I was using an HStack but I think you'll be able to do this by making enumerated 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)

        }
    }
}

In your case I think you'll make Workout hashable

struct Workout: Hashable {
    let id = UUID()
    var progress = 1
    var length: Int
    var name: String
    var description: String

    func completed() -> Int {
        return progress - 1
    }
}

and use self instead of id

List(workouts, id: \.self) { workout in

It has nothing to do with your (".id"). such List error is because some Views inside the ViewBuilder are not correct.

If I replace

 1. WorkoutDetailView()) 
 2. WorkoutBlockView(name: workout.name, workoutLength: workout.length, amountCompleted: workout.completed())
 3. WorkoutBuilderView() 

with Text , the WorkoutsView layout left is workable. Therefore, you have to figure out why those views have some problems.

    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")}

 }

 }

Hope it can help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM