如何在SwiftUI列表中更改文本数量的最大限制?

时间:2019-10-18 05:04:44

标签: swiftui swiftui-list

我通过创建一个新的空白项目发现了一个问题,列表中最多只有10个文本元素,否则会导致编译错误,我可以在任何地方编辑该限制吗?

struct ContentView: View {
    var body: some View {
        List {
            Text("Line 1")
            Text("Line 2")
            Text("Line 3")
            Text("Line 4")
            Text("Line 5")
            Text("Line 6")
            Text("Line 7")
            Text("Line 8")
            Text("Line 9")
            Text("Line 10")

            // Uncomment this line will cause Complie Error
            // Text("Line 11")
        }
    }
}

我尝试添加模型

class MyModel: ObservableObject {

    @Published var items: [String] = [
        "line 1",
        "line 2",
        "line 3",
        "line 4",
        "line 5",
        "line 6",
        "line 7",
        "line 8",
        "line 9",
        "line 10",
        "line 11",
    ]
}

并通过模型数据显示列表,显示得到修正

@ObservedObject var model = MyModel()

var body: some View {

    List {
        ForEach(model.items.indices) { index in
            Text(self.model.items[index])
        }
    }
}

所以,问题不在于列表,函数生成器吗?

使用Group更新了一种新方法,对于VStack,这是可以的,但是对于List,它将显示11行,并且每一行都与“第11行”重叠

List {
    Group {
        Text("Line 1")
        Text("Line 2")
        Text("Line 3")
        Text("Line 4")
        Text("Line 5")
        Text("Line 6")
        Text("Line 7")
        Text("Line 8")
        Text("Line 9")
        Text("Line 10")
    }

    Group {
        Text("Line 11")
    }
}

1 个答案:

答案 0 :(得分:1)

  

所以,问题不在于列表,函数生成器吗?

是的。该问题与@ViewBuilder闭包的实现方式严格相关。目前,这些闭包不能接受可变参数。因此,他们以这种方式实现了10种方法(您可以在xCode中检查这些接口):

/// Builds an empty view from an block containing no statements, `{ }`.
public static func buildBlock() -> EmptyView

/// Passes a single view written as a child view (e..g, `{ Text("Hello") }`) through
/// unmodified.
public static func buildBlock<Content>(_ content: Content) -> Content where Content : View

public static func buildBlock<C0, C1>(_ c0: C0, _ c1: C1) -> TupleView<(C0, C1)> where C0 : View, C1 : View

public static func buildBlock<C0, C1, C2>(_ c0: C0, _ c1: C1, _ c2: C2) -> TupleView<(C0, C1, C2)> where C0 : View, C1 : View, C2 : View

public static func buildBlock<C0, C1, C2, C3>(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3) -> TupleView<(C0, C1, C2, C3)> where C0 : View, C1 : View, C2 : View, C3 : View

public static func buildBlock<C0, C1, C2, C3, C4>(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4) -> TupleView<(C0, C1, C2, C3, C4)> where C0 : View, C1 : View, C2 : View, C3 : View, C4 : View

public static func buildBlock<C0, C1, C2, C3, C4, C5>(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5) -> TupleView<(C0, C1, C2, C3, C4, C5)> where C0 : View, C1 : View, C2 : View, C3 : View, C4 : View, C5 : View

public static func buildBlock<C0, C1, C2, C3, C4, C5, C6>(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6) -> TupleView<(C0, C1, C2, C3, C4, C5, C6)> where C0 : View, C1 : View, C2 : View, C3 : View, C4 : View, C5 : View, C6 : View

public static func buildBlock<C0, C1, C2, C3, C4, C5, C6, C7>(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6, _ c7: C7) -> TupleView<(C0, C1, C2, C3, C4, C5, C6, C7)> where C0 : View, C1 : View, C2 : View, C3 : View, C4 : View, C5 : View, C6 : View, C7 : View

public static func buildBlock<C0, C1, C2, C3, C4, C5, C6, C7, C8>(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6, _ c7: C7, _ c8: C8) -> TupleView<(C0, C1, C2, C3, C4, C5, C6, C7, C8)> where C0 : View, C1 : View, C2 : View, C3 : View, C4 : View, C5 : View, C6 : View, C7 : View, C8 : View

public static func buildBlock<C0, C1, C2, C3, C4, C5, C6, C7, C8, C9>(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6, _ c7: C7, _ c8: C8, _ c9: C9) -> TupleView<(C0, C1, C2, C3, C4, C5, C6, C7, C8, C9)> where C0 : View, C1 : View, C2 : View, C3 : View, C4 : View, C5 : View, C6 : View, C7 : View, C8 : View, C9 : View

如您所见,@ViewBuilder闭包最多可以使用10个参数。

解决方案取决于您的需要,如果闭包内部的视图基本上是具有不同内容的同一视图,则应在ForEach内使用List(这完全适用于此类情况) ):

struct ContentView: View {
    var body: some View {
        List {
            ForEach { val in
                //your view
            }
        }
    }
}

如果视图确实超过10种,则必须依靠Group视图将视图分为多个组:

struct ContentView: View {
    var body: some View {
        ScrollView {
            Group {
                //10 views
            }

            Group {
                //10 views
            }
        }
    }
}