SwiftUI过滤/搜索二级数据模型

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

我正在尝试使用搜索栏在SwiftUI中通过两级数据模型进行搜索/过滤:

struct CategorySection: Identifiable {
    var id = UUID()
    var title: String
    var image: String
    var color: Color
    var paintings: [Painting]
}

struct Painting {
    var title: String
    var size: String
    var year: String
    var text: String
    var show: Bool
} 

let categorySectionData: [CategorySection] = [
    CategorySection(title: "Flowers",
                    image: "Spring",
                    color: Color(#colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1)),
                    paintings: [
                        Painting(title: "Spring", size: "70 x 60cm", year: "2001", text: "Spring", show: false),
                        Painting(title: "Flowers", size: "60 x 50cm", year: "2002", text: "The", show: false),
                        Painting(title: "Corn Poppys", size: "70 x 50cm", year: "2005", text: "Year", show: false),
        ]
    ),
    CategorySection(title: "Animals",
                    image: "Deers",
                    color: Color(#colorLiteral(red: 0.2745098174, green: 0.4862745106, blue: 0.1411764771, alpha: 1)),
                    paintings: [
                        Painting(title: "Tigers", size: "70 x 60cm", year: "2001", text: "Spring", show: false),
                        Painting(title: "Deers", size: "60 x 50cm", year: "2002", text: "The", show: false),
                        Painting(title: "Puppies", size: "70 x 50cm", year: "2005", text: "Year", show: false),
        ]
    )
]

主要目的是过滤元素,例如title: "Spring"内部year: "2001"结构中的PaintingCategorySection,以绘画title s的形式在列表中显示匹配的结果,并通过NavigationLink用于在用户点击其中一个时显示的下一个视图SearchDetail

我已经尝试了以下方法来搜索第一个元素,但是Xcode却给了我很多错误。

let array = locationSectionData[0].paintings[0]
List {
   ForEach(array.filter {$0.hasPrefix(searchText) || searchText == ""}, id:\.self) { searchText in
       NavigationLink(destination: SearchDetail(item: searchText)) {
           Text(searchText)
       }
   }
}

但是,上述解决方案适用于像这样的简单数组:

let array = ["Spring", "Deer", "New Year", "Flowers"]

但不适用于多层数据结构。

理想情况下,搜索结果应返回Painting结构的整个实例,例如

Painting(title: "Spring", size: "70 x 60cm", year: "2001", text: "Spring", show: false)

,如果其中任何元素与搜索栏输入匹配,则不仅匹配单个元素。

[如果有人对我如何解决此问题有想法,那就太好了!在这一点上,我什至不确定当前的数据结构是否可行。数据结构的修改是可以接受的。

谢谢!

编辑:由于@jtouzy的帮助,我设法显示了没有过滤功能的绘画标题的完整列表。

List {
    ForEach(categorySectionData) { section in
        Section(header: Text(section.title)) {
            ForEach(section.paintings.indices, id: \.self) { painting in
                ForEach(section.paintings[painting].title.filter {$0.contains(searchText) || searchText.isEmpty}, id:\.self) { searchText in
                    NavigationLink(destination: SearchDetail(painting: section.paintings[painting])) {
                                        Text(section.paintings[painting].title)
                    }
                }
            }
        }
    }
}

但是,Xcode在行Value of type 'String.Element' (aka 'Character') has no member 'contains'上显示错误ForEach(section.paintings[painting].title.filter {$0.contains(searchText) || searchText.isEmpty}, id:\.self) { searchText in。我知道现在可能是因为它不是数组。

我正在尝试使用搜索栏在SwiftUI中通过两级数据模型进行搜索/过滤:struct CategorySection:可识别{var id = UUID()var title:String var image:String ...

swift search multidimensional-array data-structures uisearchbar
1个回答
0
投票

您的答案

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