【问题标题】:Searchbar to filter contents on a list (macOS Big Sur)用于过滤列表内容的搜索栏 (macOS Big Sur)
【发布时间】:2021-11-30 03:17:19
【问题描述】:

我正在尝试通过搜索栏实现对数据模型的搜索。

我的数据模型是以下结构:

struct NoteItem: Codable, Hashable, Identifiable {
    let id: UUID
    var text: String
    var date = Date()
    var dateText: String {
        let df = DateFormatter()
        df.dateFormat = "EEEE, MMM d yyyy, h:mm a"
        return df.string(from: date)
    }
    var tags: [String] = []
    var filename: String = ""
    var changed: Bool = false
}

应用程序迭代数据模型上的每个元素并填充一个列表,其中包含两个 TextNavigationLink:一个带有 TextEditor 的第一行,另一个带有 datamodel.dateText。列表中的每个项目都指向另一个视图,其中TextEditor 显示完整的datamodel.text

我尝试添加的搜索将仅在 datamodel.text 上。代码如下:

struct AllNotes: View {
    @EnvironmentObject private var data: DataModel
    ...

    var body: some View {
        NavigationView {
            List(data.notes) { note in
                NavigationLink(
                    destination: NoteView(note: note, text: note.text),
                    tag: note.id,
                    selection: $selectedNoteId
                ) {
                    VStack(alignment: .leading) {
                        Text(getTitle(noteText: note.text)).font(.body).fontWeight(.bold)
                        Text(note.dateText).font(.body).fontWeight(.light)
                    }
                    .padding(.vertical, 10)
                }
            }
            .listStyle(InsetListStyle())
            .frame(minWidth: 250, maxWidth: .infinity)
        }
        .toolbar {
            ToolbarItem(placement: .automatic) {
                TextField("Search...", text: $searchText)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .frame(minWidth: 200)
            }
            
        }
    }
}

搜索栏是工具栏中的文本字段。

过去,在 iOS 中,我做过类似的事情:

List(ListItems.filter({ searchText.isEmpty ? true : $0.name.contains(searchText) })) { item in
    Text(item.name)
}

但鉴于搜索栏是工具栏中的一个项目,并且我需要从数据模型中的所有文本中搜索过滤/重新填充列表,而不仅仅是列表,我什至该如何开始呢? ?如何过滤它?我需要填充一个新数组吗?如何访问搜索栏上的文本?是我发布的代码中的$searchText 吗?我执行data.filter吗?

我已经没有想法可以尝试了。

谢谢。

编辑:

我有这种工作,但请注意。它弄乱了NavigationLink 的显示方式,它在文本前添加了随机空格。

代码:

var body: some View {
        NavigationView {
            List {
                ForEach(data.notes.filter { $0.text.contains(searchText) || searchText.isEmpty }) { note in
                NavigationLink(
                    destination: NoteView(note: note, text: note.text),
                    tag: note.id,
                    selection: $selectedNoteId
                ) {
                    VStack(alignment: .leading) {
                        Text(getTitle(noteText: note.text)).font(.body).fontWeight(.bold)
                        Text(note.dateText).font(.body).fontWeight(.light)
                    }
                    .padding(.vertical, 10)
                }
            }
            .listStyle(InsetListStyle())
            .frame(minWidth: 250, maxWidth: .infinity)
            .alert(isPresented: $showAlert, content: {
                alert
            })            

            Text("Create a new note...")
                .frame(maxWidth: .infinity, maxHeight: .infinity)
            }
        }

【问题讨论】:

标签: swift macos swiftui macos-big-sur


【解决方案1】:

这是编辑后的答案,因此人们不会抱怨:

var body: some View {
        NavigationView {
                List(data.notes.filter { searchText.isEmpty ? true : $0.text.localizedCaseInsensitiveContains(searchText) }) { note in
                NavigationLink(
                    destination: NoteView(note: note, text: note.text),
                    tag: note.id,
                    selection: $selectedNoteId
                ) {
                    VStack(alignment: .leading) {
                        Text(getTitle(noteText: note.text)).font(.body).fontWeight(.bold)
                        Text(note.dateText).font(.body).fontWeight(.light)
                    }
                    .padding(.vertical, 10)
                }
            }
            .listStyle(InsetListStyle())
            .frame(minWidth: 250, maxWidth: .infinity)
            .alert(isPresented: $showAlert, content: {
                alert
            })            

        }

【讨论】:

  • @KevinSeymour 你在说什么。如果您阅读了我的问题中的代码,那么答案就很清楚了。但是因为我不想听到抱怨,所以就在这里。完整答案
  • 我同意。我想也许我按错了按钮,我已经删除了我的评论。干杯。
猜你喜欢
  • 1970-01-01
  • 2022-01-09
  • 2021-11-26
  • 2021-02-27
  • 1970-01-01
  • 2022-01-31
  • 2021-03-29
  • 1970-01-01
  • 2021-09-07
相关资源
最近更新 更多