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

问题描述

我意识到当我使用嵌套列表 - ForEach 时,我看不到这些代码中的任何内容。但是当我删除 List 时,我可以看到视图中的所有元素。不幸的是,如果不使用 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()
        }
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)