SwiftUI - 嵌套列表 [英] SwiftUI - nested list

查看:53
本文介绍了SwiftUI - 嵌套列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个嵌套的分层列表,以便对于每个任务,我都可以拥有像 iOS 提醒应用程序中那样的子任务:

I'm trying to create a nested hierarchical list, so that for each task I can have subtasks like in iOS reminders app:

第一次尝试是在列表单元格中嵌入另一个列表.

First attempt was to embed another list inside a list cell.

import SwiftUI

struct SwiftUIView: View {
    var body: some View {

        List {
            List {
              Text("Hello, World!")

            }

        }
    }
}

struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView()
    }

但是,没有成功...

有人可以帮忙吗?

干杯

推荐答案

为什么你认为它应该是 List in List... 这样的可视化表示可以只使用一个列表来生成,并且具有原生的外观 &感觉.

Why do you think it should be List in List... Such visual representation can be generated using only one list and it will have native look & feel.

这里只是一个演示(没有 UI 调整和显示/隐藏部分的逻辑,这超出了主题),但思路应该很清楚

Here is just a demo (w/o UI tuning and logic of showing/hiding sections, that is out of topic), but the idea should be clear

import SwiftUI

struct ItemRow: View {
    let category: Bool
    let text: String

    init(_ text: String, isCategory: Bool = false) {
        self.category = isCategory
        self.text = text
    }

    var body: some View {
        HStack {
            Circle().stroke() // this can be custom control
                .frame(width: 20, height: 20)
                .onTapGesture {
                    // handle tap here
                }
            if category {
                Text(self.text).bold()
            } else {
                Text(self.text)
            }
        }
    }
}

struct TestNestedLists: View {
    var body: some View {
        List { // next pattern easily wrapped with ForEach
            ItemRow("Category", isCategory: true) // this can be section's header
            Section {
                ItemRow("Item 1")
                ItemRow("Item 2")
                ItemRow("Item 3")
            }.padding(.leading, 20)
        }
    }
}

struct TestNestedLists_Previews: PreviewProvider {
    static var previews: some View {
        TestNestedLists()
    }
}

这篇关于SwiftUI - 嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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