简体   繁体   English

如何将数组中的数组放入 SwiftUI 中的 ForEach 中?

[英]How do I put Arrays inside Arrays into ForEach in SwiftUI?

I have following Code, however, I cant get the respective element to be shown.我有以下代码,但是,我无法显示相应的元素。

struct MyUserView: View {

let Users = [
["name": "Manuelle", "age": 23, "profilePicture": Image(systemName: "person"), "status": "inactive", "icon": Image(systemName: "message.fill")],
    
["name": "Michael", "age": 39, "profilePicture": Image(systemName: "person"), "status": "active 12 minutes ago", "icon": Image(systemName: "square.on.square")]
]

var body: some View {
    NavigationView {
        List {
ForEach(Users.indices, id:\.self) { user in
HStack {
Text(Data[user]["name"]!)
Image(UIImage(Data[user]["profilePicture"]!))
}
}
}

There are many, many issues.有很多很多问题。

First create a struct User for the model, the members profilePicture and icon are declared as String rather than as a SwiftUI view.首先为模型创建一个 struct User ,成员profilePictureicon被声明为String而不是 SwiftUI 视图。

struct User : Hashable {
    let name: String
    let age: Int
    let profilePicture, status, icon: String
}

In the view use the struct which is more comfortable for several reasons在视图中使用更舒适的结构有几个原因

struct MyUserView: View {
    
    let users = [
        User(name: "Manuelle", age: 23, profilePicture: "person", status: "inactive", icon: "message.fill"),
        User(name: "Michael", age: 39, profilePicture: "person", status: "active 12 minutes ago", icon: "square.on.square")
    ]
    
    var body: some View {
        NavigationView {
            List {
                ForEach(users, id: \.self) { user in
                    HStack {
                        Text(user.name)
                        Image(systemName: user.profilePicture)
                    }
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM