首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对两个不同的listData使用相同的结构

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

Stack Overflow用户
提问于 2021-02-10 20:48:13
回答 1查看 40关注 0票数 0

我有这个ListPage结构

代码语言:javascript
复制
    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的listData,名为WalkthroughList,如下所示

代码语言:javascript
复制
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,这样我才能对它们都使用这个结构。

下面的代码是错误的,但是为了显示我想要做的事情

代码语言:javascript
复制
    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))
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-10 21:26:26

最简单的方法可能是让它们共享一个父级。但是你也可以试试protocol

代码语言:javascript
复制
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")
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66137211

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档