打破SwiftUI中List的最大容量

oOE*_*ric 1 ios swift swiftui

我在列表中添加了10个HStack。当我添加第11个HStack时,Xcode显示错误。

该页面教我如何使用组突破10个孩子的限制。但这对我没有用。如何在列表中添加第11个HStack?


作品:

        List {

            Text("????????")

            HStack {
                Text("?")
                TextField("?", text: self.$name)
            }

            ...

            HStack {
                Text("????")
                TextField("??????/??", text: self.$name)
            }

    }
Run Code Online (Sandbox Code Playgroud)

无法运作:

        List {

            Text("????????")

            HStack {
                Text("?")
                TextField("?", text: self.$name)
            }

            ...

            HStack {
                Text("????")
                TextField("??????/??", text: self.$name)
            }

            HStack { //The extra HStack
                Text("????2")
                TextField("??????/??", text: self.$name)
            }

    }
Run Code Online (Sandbox Code Playgroud)

出现以下错误消息:

    <unknown>:0: error: ambiguous reference to member 'buildBlock()'
SwiftUI.ViewBuilder:3:24: note: found this candidate
    public static func buildBlock() -> EmptyView
                       ^
SwiftUI.ViewBuilder:4:24: note: found this candidate
    public static func buildBlock<Content>(_ content: Content) -> Content where Content : View
                       ^
SwiftUI.ViewBuilder:3:24: note: found this candidate
    public static func buildBlock<C0, C1>(_ c0: C0, _ c1: C1) -> TupleView<(C0, C1)> where C0 : View, C1 : View
                       ^
Run Code Online (Sandbox Code Playgroud)

如果使用分组,则可以编译该应用程序,但文本会重叠(列表看起来不符合预期;请查看下图):

        List {

        Group {

            Text("????????")

            HStack {
                Text("?")
                TextField("?", text: self.$name)
            }

            ...

            HStack {
                Text("????")
                TextField("??????/??", text: self.$name)
            }

        }

        Group {
            HStack {
                Text("????2")
                TextField("??????/??", text: self.$name)
            }
        }

    }
Run Code Online (Sandbox Code Playgroud)

重叠文字

Ket*_*dra 5

在这里,您的解决方案:

制作GroupHStack

   List {

            Text("????????")

            HStack { //1
                Text("?")
                TextField("?", text: self.$name)
            }

            HStack { //2
                Text("????")
                TextField("??????/??", text: self.$name)
            }

            HStack { //3
                Text("????2")
                TextField("??????/??", text: self.$name)
            }

            HStack { //4
                Text("????3")
                TextField("??????/??", text: self.$name)
            }

            HStack { //5
                Text("????4")
                TextField("??????/??", text: self.$name)
            }

            //Make Group Here
            Group {

                HStack { //6
                    Text("????5")
                    TextField("??????/??", text: self.$name)
                }

                HStack { //7
                    Text("????6")
                    TextField("??????/??", text: self.$name)
                }

                HStack { //8
                    Text("????7")
                    TextField("??????/??", text: self.$name)
                }

                HStack { //9
                    Text("????8")
                    TextField("??????/??", text: self.$name)
                }

                HStack { //10
                    Text("????9")
                    TextField("??????/??", text: self.$name)
                }

                HStack { //11
                    Text("????10")
                    TextField("??????/??", text: self.$name)
                }
            }

        }.padding()
Run Code Online (Sandbox Code Playgroud)

但是正如@kontiki所说的那样,

您正在重复一个模式。因此,将其制成阵列。并使用ForEach