簡體   English   中英

.onDelete 不調用 function swiftUI

[英].onDelete not calling function swiftUI

我正在嘗試刪除列表中核心數據 object 數組的成員。 我正在使用 function 中內置的滑動刪除來執行此操作 (.onDelete),但是當我調試我的代碼時(未顯示任何錯誤)它根本不調用 function(我的斷點都沒有命中)。 沒有錯誤。

我已經嘗試了許多不同的 .onDelete 實現,但似乎沒有一個真正觸發了 .onDelete。

這是我的代碼

//
//  FilteredList.swift
//  Workout
//
//  Created by Kaden Thompson on 3/9/22.
//

import SwiftUI
struct FilteredList: View {
    @Environment(\.managedObjectContext) var moc
    @FetchRequest var fetchRequest: FetchedResults<Workout>
    var filter: String
    init(filterIn: String) {
        filter = filterIn
        _fetchRequest = FetchRequest<Workout>(sortDescriptors: [SortDescriptor(\.order, order: .reverse)], predicate: NSPredicate(format: "type = %@", filterIn))
    }
    var body: some View {
        
        NavigationView{
        List() {
            ForEach(fetchRequest, id: \.self) { workout in
                let dateString: String = workout.date ?? ""
                let dateStringTitleStep = dateString.firstIndex(of: " ")!
                let dateStringTitle: String = String(dateString[...dateStringTitleStep])
                let dateStringTime: String = String(dateString[dateStringTitleStep...])
                let weight: String = workout.weight ?? ""
                let reps: String = workout.rep ?? ""
                if (workout.last){

                    Text(dateStringTitle).font(.title).fontWeight(.bold).padding(.vertical, 20.0)
                }
                HStack{
                    
                    let displayString: String = "Time: " + dateStringTime.replacingOccurrences(of: " ", with: "")
                    Text(displayString).bold()
                    Spacer()
                    if(filter == "Elliptical"){
                        let displayString2: String = "Time: " + weight + "\nResistance: " + reps
                        Text(displayString2)
                    }
                    else{
                        let displayString2: String = "Weight: " + weight + "\nReps: " + reps
                        Text(displayString2)
                    }
                    Spacer()
                }
                
            }
            .onDelete(perform: deleteWorkouts)
            }

        .navigationTitle(filter.replacingOccurrences(of: "_", with: " "))
        .navigationBarItems(leading: Image(filter).resizable().frame(width:40, height:40, alignment: .center).cornerRadius(20))
        
        }

    }
    func deleteWorkouts(at offsets: IndexSet) {
        for offset in offsets {
            // find this book in our fetch request
            let workout = fetchRequest[offset]

            // delete it from the context
            moc.delete(workout)
        }

        // save the context
        try? moc.save()
    }
    
}

這個問題似乎是由於

  if (workout.last){..}

你可以嘗試使用

if (workout.last){..} else { 
  HStack {...} 
}

或者您可以嘗試將ForEach的內部包裹在VStack中,例如:

 ForEach(fetchRequest, id: \.self) { workout in
    VStack {
     ...
    }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM