如何在 Mac 上处理 SwiftUI 表中的动态列名称

发布于 2025-01-21 02:05:05 字数 695 浏览 2 评论 0原文

我有一个 SwiftUI 表,需要根据来自服务器的一些请求数据进行布局。该数据是动态的并且会变化。但是,一旦我将这些数据读入数组,它就不允许我对列数据执行 forEach,因为它不符合“TableColumnContent”。是否有另一种方法来循环此创建,或者是否有一种简单的方法来符合此协议?当我查看该协议时,我很难理解它。

struct DataProperty : Identifiable
{
    var id: String
    var name: String
}

struct TableDataView : View
{
    @EnvironmentObject var cache: ServerCache
    
    var body: some View
    {
        Table(cache.activeTableData)
        {
            ForEach(cache.activeProperties, id: \.self) {property in
                
                TableColumn(property.name, value: \.id)
                
            }
            
        }
    }
}

请注意,activeProperties 是 DataProperty 的数组。我在这里做错了什么?

I have a SwiftUI table that I need to layout based on some request data from a server. This data is dynamic and changes. However, once I read this data into an array, it will not allow me to do a forEach on the column data, as it companies about not conforming to "TableColumnContent". Is there another way to loop this creation, or is there a simple way to conform to this protocol? I am having trouble understanding that protocol when I look at it.

struct DataProperty : Identifiable
{
    var id: String
    var name: String
}

struct TableDataView : View
{
    @EnvironmentObject var cache: ServerCache
    
    var body: some View
    {
        Table(cache.activeTableData)
        {
            ForEach(cache.activeProperties, id: \.self) {property in
                
                TableColumn(property.name, value: \.id)
                
            }
            
        }
    }
}

Note that activeProperties is an array of DataProperty. What am I doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文