简体   繁体   English

SwiftUI 列出多个自定义行编辑选项

[英]SwiftUI list multiple custom row edit option

I need a multiple rows edit option for SwiftUI list我需要 SwiftUI 列表的多行编辑选项

  1. My login screen in NavigationView我在 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也保持简单的行后,编辑不起作用

EditButton tracks edit mode automatically, so just remove your explicit state and all works (on replicated code, Xcode 11.4) EditButton自动跟踪编辑模式,因此只需删除显式 state 和所有工作(在复制代码上,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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM