0

我正在尝试.delete在列表中使用 SwiftUI 的方法来识别HKWorkout用户尝试删除的内容,但是由于我将锻炼打包到自定义 WorkoutMonth 对象中,我很难弄清楚如何深入了解特定的锻炼用户正在点击?在 UIKIT 中,我能够使用indexPath.sectionindexPath.row找到它,但在这里我迷路了。

struct MonthsWorkoutsListView: View {
    
    
    @Environment(\.colorScheme) var colorScheme
    @EnvironmentObject var trackerDataStore: TrackerDataStore
    @State var workouts: [TrackerWorkout]
    @State var workoutMonths = [WorkoutMonth]()
    
    var body: some View {
        VStack {
            if workoutMonths.count == 0 {
                Text("No workouts logged yet.  Open the Athlytic App on your Apple Watch to start and save a new workout.")
                    .multilineTextAlignment(.center)
                    .padding(.horizontal)
            } else {
                List {
                    ForEach(workoutMonths) { workoutMonth in
                        Section(header: Text(getFormattedYearMonth(workoutMonth: workoutMonth))) {
                            ForEach(workoutMonth.trackerWorkouts) { workout in
                                NavigationButton(
                                    action: {
                                        trackerDataStore.selectedWorkout = workout //this line is needed so that the image  at the top of WorkoutDetailHeaderCard gets updated,  otherwise the view loads before the async called and you end up with the  image of the last selected workout
                                        trackerDataStore.loadDataForSelectedWorkout(selectedWorkoutToBeUpdated: workout)
                                    },
                                    destination: {
                                        WorkoutDetailView().environmentObject(trackerDataStore)
                                    },
                                    workoutRow: { WorkoutRow(trackerWorkout: workout) }
                                )
                            }
                        }
                    }
                    .onDelete(perform: delete)
                }
                .navigationBarTitle(Text("Workouts"))
            }
        }
        .onAppear {
            workoutMonths = buildWorkoutsIntoYearMonths(workouts: workouts)
        }
    }
    
    func delete(at offsets: IndexSet) {

        //How to look up the workout user wants to delete?

    }

struct YearMonth: Comparable, Hashable {
    
    let year: Int
    let month: Int
    
    init(year: Int, month: Int) {
        self.year = year
        self.month = month
    }
    
    init(date: Date) {
        let comps = Calendar.current.dateComponents([.year, .month], from: date)
        self.year = comps.year!
        self.month = comps.month!
    }
    
    static func < (lhs: YearMonth, rhs: YearMonth) -> Bool {
        if lhs.year != rhs.year {
            return lhs.year < rhs.year
        } else {
            return lhs.month < rhs.month
        }
    }
}

struct WorkoutMonth: Identifiable {
    var id = UUID()
    var yearMonth: YearMonth
    var trackerWorkouts: [TrackerWorkout]
}
4

2 回答 2

-1

我想通了,我把我.delete的放在外面forEach,把它放在这里让我可以访问workoutMonth

ForEach(workoutMonth.trackerWorkouts) { workout in
    NavigationButton(
        action: {
            trackerDataStore.selectedWorkout = workout
            trackerDataStore.loadDataForSelectedWorkout(selectedWorkoutToBeUpdated: workout)
        },
        destination: {
            WorkoutDetailView().environmentObject(trackerDataStore)
        },
        workoutRow: { WorkoutRow(trackerWorkout: workout) }
    )
}
.onDelete { (offSets) in
    for offset in offSets {
    print("user wants to delete workout \(workoutMonth.trackerWorkouts[offset].startDate)")
    }
}
于 2021-01-29T21:01:41.963 回答
-1

您已IndexSet作为参数传递。这个可以用来消除适当的锻炼

workoutMonths.remove(atOffsets: offsets)

如果要收集所有应该删除的对象,也可以使用 foreach 循环进行迭代。然后,您可以访问将要删除的每个对象。

for offset in offsets {
   var workoutMonth: WorkoutMonth = workoutMonths[offset]
}
于 2021-01-12T20:12:16.777 回答