List ForEach 问题 SwiftUI - ListCells 在 List 中不可见

问题描述 投票:0回答:1

我意识到,当我使用嵌套列表 - ForEach 时,我看不到这些代码中的任何内容。但是当我删除列表时,我可以看到视图中的所有元素。不幸的是,如果不使用 List,我就无法使用 .onDelete,这是一个问题。

    struct ListsInfoView: View {
    @Environment(\.managedObjectContext) private var viewContext
    @FetchRequest(entity:CDListModel.entity(),
                  sortDescriptors: [
                    NSSortDescriptor(
                        keyPath:\CDListModel.title,
                        ascending: true )
                  ]
    )var lists: FetchedResults<CDListModel>
    @State var isListSelected = false
    @State var selectedList : CDListModel!
    var body: some View {
            List{
                ForEach(lists) { list in
                    Button(action: {
                        self.selectedList = list
                        self.isListSelected.toggle()
                    }) {
                        ListCell(list: list)
                    }
                }
                .onDelete(perform: deleteList)
            }
            .listStyle(PlainListStyle())
            .fullScreenCover(isPresented: $isListSelected) {
                ListDetailView(selectedList: $selectedList)
                    .environment(\.managedObjectContext, viewContext)
            }
    }
    func deleteList(at offsets: IndexSet) {
        viewContext.delete(lists[offsets.first!])
        PersistenceController.shared.saveContext()
    }
}

像上面一样,我没有看到任何 ListCell,但是当我删除 List { } 时,一切都完美了。这是为什么?

我的列表单元格

    struct ListCell: View {
    @ObservedObject var list : CDListModel
    var body: some View {
        HStack{
            Image(systemName: "folder")
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(width: 30, height: 30, alignment: .center)
                .foregroundColor(.yellow)
            Text(list.title ?? "")
                .foregroundColor(.black)
                .font(.system(size: 20, weight: .regular, design: .rounded))
                .padding(.leading,10)
            Spacer()
            Text(String(list.notes?.count ?? 0))
                .foregroundColor(.gray)
            Image(systemName: "chevron.right")
                .foregroundColor(.gray)
        }
        .padding(.horizontal)
    }
}

这是我的 MainView,我称之为 ListsView。在 ListView 内部,如果 isShowTapped

,我将调用 ListInfoView
struct MainView: View {
@Environment(\.managedObjectContext) private var viewContext
@State var searchText = ""
@State var newFolderName = ""
@State var isAddList : Bool = false
@State var isAddNote: Bool = false
var body: some View {
    ZStack{
        Color(.white)
            .edgesIgnoringSafeArea(.all)
        NavigationView{
            VStack(alignment: .leading){
                ScrollView{
                    SearchBar(text: $searchText)
                        .environment(\.managedObjectContext, viewContext)
                    ListsView()
                        .environment(\.managedObjectContext, viewContext)

列表视图

    struct ListsView: View {
    @Environment(\.managedObjectContext) private var viewContext
    @State var isShowTapped: Bool = false
    @State var selectedIndex : Int = 0
    var body: some View {
        VStack {
            Spacer()
            HStack{
                Text("On My iPhone")
                    .font(.system(size: 20, weight: .semibold, design: .rounded))
                Spacer()
                Button(action: {
                    withAnimation{
                    self.isShowTapped.toggle()
                    print("slider button tapped")
                    }
                }, label: {
                    Image(systemName:isShowTapped ? "chevron.down" : "chevron.right")
                        .foregroundColor(.black)
                })
            }
            .padding(.horizontal)
            if isShowTapped {
                ListsInfoView()
                    .environment(\.managedObjectContext, viewContext)
                    .transition(.scale)
            } else {}
            Spacer()
        }
    }
}
list foreach swiftui
1个回答
0
投票

ScrollView 就是问题所在。 GL!

© www.soinside.com 2019 - 2024. All rights reserved.