Binding 对两个不同的listData使用相同的结构

Binding 对两个不同的listData使用相同的结构,binding,swiftui,Binding,Swiftui,我有这个ListPage结构 struct ListPage: View { @Binding var page: Int var body: some View { HStack(spacing: 0) { ForEach(BannerList.listData) { i in BannerCell(page: self.$page, width: UIScreen.main.bounds.widt

我有这个ListPage结构

    struct ListPage: View {
    @Binding var page: Int
    var body: some View {
        HStack(spacing: 0) {
            ForEach(BannerList.listData) { i in
                BannerCell(page: self.$page, width: UIScreen.main.bounds.width, item: i)
            }
        }
    }
}
但现在我有另一个列表数据作为BannerList,名为WalkthroughList,如下所示

struct WalkthroughList {
    static let listData: [Walkthrough] = [
        Walkthrough(image: "", title: Constants.WalkthroughTitles.walkthrough1, subtitle: Constants.WalkthroughSubtitle.walkthroughSubtitle1),
        Walkthrough(image: "", title: Constants.WalkthroughTitles.walkthrough2, subtitle: Constants.WalkthroughSubtitle.walkthroughSubtitle2),
        Walkthrough(image: "", title: Constants.WalkthroughTitles.walkthrough3, subtitle: Constants.WalkthroughSubtitle.walkthroughSubtitle3)
    ]
}

struct BannerList {
static let listData: [Banner] = [
    Banner(elementID: 0, title: Constants.BannerNames.banner1, color: Constants.Colors.gray),
    Banner(elementID: 1, title: Constants.BannerNames.banner2, color: Constants.Colors.red),
    Banner(elementID: 2, title: Constants.BannerNames.banner3, color: Constants.Colors.blue)
]
}

我应该如何修改我的ListPage,以便可以将此结构用于这两个页面

下面的代码是错误的,但显示我想做什么

    struct ListPage: View {
    @Binding var page: Int
    var listData = [Any]()
    var cell: Any
    var body: some View {
        HStack(spacing: 0) {
            ForEach(listData) { i in
                cell(page: self.$page, width: UIScreen.main.bounds.width, item: i)
            }
        }
    }
}

struct List_Previews: PreviewProvider {
    static var previews: some View {
        ListPage(page: .constant(1), listData: BannerList.listData, cell: Banner(elementID: 0, title: Constants.BannerNames.banner1, color: Constants.Colors.green))
    }
}

最简单的方法可能是让他们共享一个父母。但是你也可以尝试一个
协议

class CommonObject: Identifiable{
    let id: UUID = UUID()
    var title: String
    
    init(title: String) {
        self.title = title
    }
}

class Walkthrough: CommonObject{
    var image: String
    var subtitle: String
    
    init(image: String, title: String, subtitle: String) {
        self.image = image
        self.subtitle = subtitle
        super.init(title: title)
    }
}

class Banner: CommonObject{
    
    var elementID: Int
    var color: Color
    
    init(elementID: Int, title: String, color: Color) {
        self.elementID = elementID
        self.color = color
        super.init(title: title)
    }
    
}

struct SharedList: View {
    @Binding var page: Int
    var listData: [CommonObject] = BannerList.listData + WalkthroughList.listData
    
    var body: some View {
        HStack(spacing: 0) {
            ForEach(listData) { i in
                CommonCell(page: self.$page, width: UIScreen.main.bounds.width, item: i)
            }
        }
    }
}

struct CommonCell: View {
    @Binding var page: Int
    let width: CGFloat
    let item: CommonObject
    var body: some View {
        if item is Banner{
            Text(item.title + " I am a Banner")
                //You can cast them back to their original state here
                .foregroundColor((item as! Banner).color)
            
        }else if item is Walkthrough{
            Text(item.title + " I am a Walkthrough")
        }else{
            Text(item.title + " unknown CommonObject")
        }
    }
}