从列表中删除行时出现“索引超出范围”错误

时间:2020-06-14 16:21:24

标签: arrays swift foreach swiftui indexoutofrangeexception

在搜索这些论坛时,这似乎是一个常见问题。我的应用程序非常简单,所以我不确定为什么会这样。尝试从列表中删除行时,出现“索引超出范围”错误。移动行工作正常。编辑行也可以。

根据我的研究,我认为这是ForEach的问题,但不确定。

class PatientData: ObservableObject, Identifiable
{
    let id = UUID()

    @Published var patientName = ""
    @Published var mrNumber = ""
    @Published var age = ""
    @Published var dateOfBirth = ""

    init(patientName: String, mrNumber: String, age: String, dateOfBirth: String)
    {
        self.patientName = patientName
        self.mrNumber = mrNumber
        self.age = age
        self.dateOfBirth = dateOfBirth
    }
}

var patientDataArray: [PatientData] = []



final class PatientDataController: ObservableObject, Identifiable
{
    @Published var patients = patientDataArray
}
struct PatientList: View
{
    @EnvironmentObject var patientDataController: PatientDataController
    @EnvironmentObject var patientData: PatientData

    @State var newPatient: PatientData = PatientData(patientName: "", mrNumber: "", age: "", dateOfBirth: "")

    @State private var showModalSheet = false

    var body: some View
    {
        NavigationView
        {
            List
            {   
                ForEach(patientDataController.patients.indices, id: \.self)
                { index in
                NavigationLink(destination: PatientInfoCard(patientData: self.patientDataController.patients[index]))
                { PatientListCell(patientData: self.$patientDataController.patients[index]) }
                }
                .onMove(perform: move)
                .onDelete(perform: delete)

                .navigationBarTitle(Text("Patient List"))
            }
        .navigationBarItems(leading: EditButton(), trailing: Button(action:
            {self.showModalSheet.toggle()})
                {Text("New")})
                .sheet(isPresented: $showModalSheet)
                {
                    NewPatientModal(newPatient: self.$newPatient, showModalSheet: self.$showModalSheet)
                        .environmentObject(self.patientDataController)
                }                        
        }
    }

    private func delete(offsets: IndexSet)
    {
        patientDataController.patients.remove(atOffsets: offsets)

    }

    private func move(source: IndexSet, destination: Int)
    {
        patientDataController.patients.move(fromOffsets: source, toOffset: destination)
    }
}
struct PatientInfoCard: View
{
    @EnvironmentObject var patientDataController: PatientDataController

    @State var patientData: PatientData
    @State private var showModalSheet = false

    var patientDataIndex: Int
        {
        patientDataController.patients.firstIndex(where: { $0.id == patientData.id })!
        }

    var body: some View
    {
        VStack(alignment: .leading, spacing: 8)
        {  
            Text(patientData.patientName)
                .font(.largeTitle)
            Group
            {
                Text("MR #:")
                    .font(.headline)
                Text(patientData.mrNumber)
                    .font(.subheadline)
                    .foregroundColor(.secondary)
                Divider()
                Text("Age:")
                    .font(.headline)
                Text(patientData.age)
                    .font(.subheadline)
                    .foregroundColor(.secondary)
                Divider()
                Text("DOB:")
                    .font(.headline)
                Text(patientData.dateOfBirth)
                    .font(.subheadline)
                    .foregroundColor(.secondary)
            }
            Spacer()
                .frame(minWidth: 0, maxWidth: .infinity)
        }
        .padding()

        .navigationBarItems(trailing: Button(action:
            {self.showModalSheet.toggle()})
                {Text("Edit")})
            .sheet(isPresented: $showModalSheet)
        {
            EditPatientModal(patientData: self.$patientData, showModalSheet: self.$showModalSheet)
                .environmentObject(self.patientDataController)
        }
    }
}

0 个答案:

没有答案