无法添加超过 10 个项目来查看 SwiftUI [英] Can't Add More Than 10 Items to View SwiftUI

查看:33
本文介绍了无法添加超过 10 个项目来查看 SwiftUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在完成一个需要用户数据输入字段的应用.我用少量数据元素对其进行建模以简化开发.今天我尝试添加其他元素,但惊讶地发现我只能向一个视图添加 10 个视图.所以我尝试了最简单的设计(如下).如果我在视图中添加了 11 个事物",它会立即在顶部项目上显示错误,无论它是什么:

I'm finishing an app that needs user data entry fields. I modeled it with a small number of data elements to streamline the development. Today I attempted to add additional elements and was astounded to find that I could only add 10 views to a view. So I tried the simplest of designs (below). If I added 11 "things" to the view it immediately presented the error on the top item, whatever it was:

传递给不带参数的调用的参数"

"Argument passed to call that takes no arguments"

我将外部容器称为 ScrollView、VStack、List 或 Form 都没有关系.同样的行为.Text/TextField 子单元是否在 VStack 中无关紧要.

Doesn't matter whether I call the outside container a ScrollView, VStack, List or Form. Same behavior. Doesn't matter whether the Text/TextField sub units are in a VStack or not.

所以我回到了基础 - 只添加了十个文本视图.没问题.添加第十一个,它会爆炸.这是其中一种变体 - 但我需要做的就是添加 10 个简单的文本视图以使其中断.

So I went back to basics - just added ten Text views. No problem. Add an eleventh and it blows up. Here's one of the variants - but all I need to do was add 10 simple Text views to get it to break.

我一定在这里遗漏了一些非常基本的东西.我检查了较新版本的 Xcode,但我有最新版本 11.2 beta 2 (11B44).

I must be missing something really basic here. I checked for a newer release of Xcodebut I have Version 11.2 beta 2 (11B44), the latest.

@State private var textField1: String = "Pass to the ListCell"
@State private var textField2: String = "2"
//more of these


var body: some View {

    NavigationView {
        VStack {

            //extract the VStack and create a separate struct
            ListCell(tfString: textField1)

            VStack {
                Text("Text Field")
                TextField("Placeholder", text: $textField2)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .padding()
            }

            VStack {
                Text("Text Field")
                TextField("Placeholder", text: $textField3)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .padding()
            }

            //more of the above VStacks

            Text("6")
            Text("7")
            Text("8")
            Text("9")
            Text("10")
            //Spacer()
            //Text("11")
        }
    }
}

任何指导将不胜感激.

推荐答案

Use Group {...} https://developer.apple.com/documentation/swiftui/group

var body: some View {

    NavigationView {
        VStack {

            //extract the VStack and create a separate struct
            ListCell(tfString: textField1)
            Group {
                VStack {
                    Text("Text Field")
                    TextField("Placeholder", text: $textField2)
                        .textFieldStyle(RoundedBorderTextFieldStyle())
                        .padding()
                }

                VStack {
                    Text("Text Field")
                    TextField("Placeholder", text: $textField3)
                        .textFieldStyle(RoundedBorderTextFieldStyle())
                        .padding()
                }
            }

            //more of the above VStacks
            Group {
                Text("6")
                Text("7")
                Text("8")
                Text("9")
                Text("10")
            }
            //Spacer()
            //Text("11")
        }
    }
}

这篇关于无法添加超过 10 个项目来查看 SwiftUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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