首页 > 解决方案 > 无法在列表中反映我添加的项目

问题描述

我的代码可以正确运行和编译,但我看不到任何已添加到列表中的项目。我知道它们是vitallist数组的一部分,因为我可以打印它们并看到它们已被添加,但我无法弄清楚为什么它们没有显示在ListView. 这可能与ObservableObject Protocol.

struct Vital: Identifiable {
        let id = UUID()
        var name: String
    }

class VitalList:ObservableObject {
   @Published var vitallist = [Vital]()
}

struct ItemAdd: View {
    @Environment(\.presentationMode) var presentationMode
    @ObservedObject var items1: VitalList


    @State private var item =  ""

    @State var itemvalue: Int = 0

    var body: some View {

            NavigationView{
                HStack{
                    TextField("Item", text: $item)
                        .padding()
                    Button(action:{

                        if self.itemvalue == 1{
                            let things1 = Vital(name: self.item)
                        self.items1.vitallist.append(things1)
                        self.presentationMode.wrappedValue.dismiss()

                        }

                        else{
                            let things4 = Delegate(name: self.item)
                          self.items4.delegatelist.append(things4)
                            self.presentationMode.wrappedValue.dismiss()
                        }

                    }){
                        Image(systemName:"paperplane")
                            .font(.headline)
                            .padding()
                    } 
        }

    }
}



struct Row: View {
    var vital: Vital

    @State var completed:Bool = false
    var body: some View {
        HStack{
            Image(systemName: completed ? "checkmark.circle.fill" : "circle").onTapGesture {
                self.completed.toggle()
            }
            Text(vital.name)
        }
    }
}



struct Lists: View {

    @ObservedObject var vitallist = VitalList()


    var body: some View {
        NavigationView{
            List{
                Section(header: Text("Vital")){
                    ForEach(vitallist.vitallist){ item in
                        Row(vital: item)
                    }
                }
            }.navigationBarTitle("ToDo")
        }

    }
}

struct ContentView: View {
    @ObservedObject var vital = VitalList()


    @State private var  showingAdditem: Bool = false

    var body: some View {
        ZStack{
            Lists()
            VStack{
                               Spacer()

                               HStack{
                                   Spacer()

                                   Button(action:{
                                       self.showingAdditem = true
                                   }){
                                       Image(systemName: "plus")
                                           .font(Font.system(size: 25,weight: .regular))
                                           .font(.largeTitle)
                                           .frame( width: 50, height: 50)
                                           .foregroundColor(.white)
                                   }.sheet(isPresented: $showingAdditem)
                                   {
                                       ItemAdd(items1: self.vital, items2: self.urgent, items3: self.important, items4: self.delegate)
                                   }.background(Color.blue)
                                       .cornerRadius(25)
                                       .shadow(color: Color.black.opacity(0.3), radius: 3,
                                               x: 3,
                                               y: 3)
                                       .padding()
                                       .offset(x: -5,
                                               y: 15)
                               }
                           }
        }
    }
}

未显示项目的列表视图

标签: arraysswiftlistswiftui

解决方案


推荐阅读