您如何在ForEach Swiftui中的结构列表中使用枚举? [英] How do you use enumerated in a list of structs in ForEach Swiftui?

查看:70
本文介绍了您如何在ForEach Swiftui中的结构列表中使用枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望利用ForEach中各项的索引.这是关于我发表的帖子这里,现在我将buttonTitles更改为字典中的结构列表.但是我似乎无法使用[struct]在ForEach中执行通常的.enumerated()方法.

I wish to utilise the index from the items in ForEach. This is regarding the post I made here where I have now changed the buttonTitles into a list of struct from a dictionary. However I can't seem to do the usual .enumerated() method in ForEach with [struct].

struct ButtonObject: Hashable{
    let id =  UUID()
    var name: String
    var isSelected: Bool

}


class SomeData: ObservableObject{
    @Published var buttonObjects: [ButtonObject] = [ButtonObject(name: "tag1", isSelected: false),
                                                   ButtonObject(name: "tag2", isSelected: false), ButtonObject(name: "tag3", isSelected: false)]
}



struct someData3: View {
    @ObservedObject var someData = SomeData()

    var body: some View {
        VStack{
            ForEach(Array(someData.buttonObjects.enumerated()), id: \.element.id)){ind, object in
                HStack{
                    Text(ind)
                    Text(object.name)
                }
            }




        }

    }
}

推荐答案

此处为固定部分

ForEach(Array(someData.buttonObjects.enumerated()), id: \.element.id) { ind, object in
    HStack{
        Text("\(ind)")
        Text(object.name)
    }
}

这篇关于您如何在ForEach Swiftui中的结构列表中使用枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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