list - 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)
}
}
这是我称之为 ListsView 的 MainView。在 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()
}
}
}
解决方案
推荐阅读
- android - Flutter gRPC:Android 应用中双向 TLS 的最佳方法
- python - Python - 仅在 Linux 终端中输入时出现 EOF 错误
- javascript - 更新表格的背景图像而不会“闪烁”
- typescript - 如何使用 patch-package 修补 TSX 文件
- node.js - Vuejs 中没有未使用的变量
- sql - 如何查询 cosmosdb 中的数字范围?
- django - 如何通过 django 在电子邮件中发送图像
- c# - T4 文本模板 - 导入和使用自己的代码
- airflow - 气流似乎正在丢失任务并认为它们失败了
- javascript - 如何在不刷新的情况下在javascript中显示数据以每2-5分钟运行一次