对两个不同的listData使用相同的结构 [英] Using same struct for two different listData

查看:44
本文介绍了对两个不同的listData使用相同的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个ListPage结构

I have this ListPage struct

    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)
            }
        }
    }
}

但是现在我有另一个listData作为BannerList,名为WalkthroughList,如下所示

But now I have another listData as BannerList named WalkthroughList as below

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以便可以将它们都用于此结构.

How should I modify my ListPage so I can use this struct for both of them.

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

This code below is wrong but to show what I want to do

    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))
    }
}

推荐答案

最简单的方法可能是让它们共享一个父级.但是您也可以尝试 Protocol .

The easiest way is probably having them share a parent. But you can try a protocol as well.

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")
        }
    }
}

这篇关于对两个不同的listData使用相同的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆