1
votes

I need a multiple rows edit option for SwiftUI list

  1. My login screen in NavigationView
  2. After login Home screen is tab bar
  3. one of the tab is list view (I need edit option for this list)

Here is my total code

first screen I have only simple navigation view with text fields and button.

struct HomeView: View {
    
    @ObservedObject var viewModel = HomeViewModel()
    
    //@State var title = "Home"
    
    var body: some View {
        TabView(selection: $viewModel.selectedView) {
            TasksView()
            .tabItem {
                Image(“one”)
                Text(“one”)
            }.tag(0)
            DashboardView()
            .tabItem {
                Image(“two”)
                Text(“two”)
            }.tag(1)
            NotifcationsView()
            .tabItem {
                Image(“some”)
                Text(“some”)
            }.tag(2)
            SettingsView()
            .tabItem {
                Image(“set”)
                Text("Se")
            }.tag(3)
        }
        .navigationBarBackButtonHidden(true)
        .navigationBarItems(trailing: EditButton())
        .navigationBarTitle(Text(viewModel.title) , displayMode: .inline)
    }
    
}


struct TasksView: View {
    
    @ObservedObject var viewModel = TViewModel()
    @State var segmentSelection = 0
    @State var selection = Set<String>()
//    @State var editMode = EditMode.active
    
    var body: some View {
        VStack {
            Picker(selection: $viewModel.segmentSelection, label: Text("")) {
                ForEach(0..<self.viewModel.segments.count) {index in
                    Text(self.viewModel.segments[index]).tag(index)
                }
            }.pickerStyle(SegmentedPickerStyle())
                .padding(5)
            //MyTaskListView()
            List (selection: $selection) {
                ForEach(viewModel.mt){ t in
                    //TaskCell(t : task)
                    Text("Title")
                }
                .onDelete(perform: viewModel.delete)
            }
        }.onAppear{
            self.viewModel.requestMYTasks()
        }
        .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
        
    }
    
    init() {
        UISegmentedControl.appearance().selectedSegmentTintColor = Colors.navBarColor
        UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.white], for: .selected)
        UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: Colors.navBarColor], for: .normal)
    }
    
    
}

after keeping simple row also , edit is not working

1

1 Answers

0
votes

EditButton tracks edit mode automatically, so just remove your explicit state and all works (on replicated code, Xcode 11.4)

//@State var editMode = EditMode.active         // remove this

List (selection: $selection) {
                ForEach(viewModel.myTasks){ task in
                    TaskCell(task : task)
                }
                .onDelete(perform: viewModel.delete)
//                .environment(\.editMode, self.$editMode)    // ... and this
            }
.navigationBarItems(trailing: EditButton())