• swift - 如何在数组大小更改后刷新 ForEach 显示元素的数量(SwiftUI、Xcode 11 Beta 5)


    我正在尝试实现一个 View ,该 View 可以在内容数组的大小发生变化时更改显示项目的数量(由 ForEach 循环创建),就像购物应用程序可能会在用户下拉刷新后更改其可用项目的数量一样

    这是我到目前为止尝试过的一些代码。如果我没记错的话,这些适用于 Xcode beta 4,但适用于 beta 5:

    • 如果数组的大小增加,循环仍将显示原始数量的元素
    • 数组的大小减小会导致索引超出范围错误

    代码:

    1. import SwiftUI
    2. struct test : View {
    3. @State var array:[String] = []
    4. @State var label = "not pressed"
    5. var body: some View {
    6. VStack{
    7. Text(label).onTapGesture {
    8. self.array.append("ForEach refreshed")
    9. self.label = "pressed"
    10. }
    11. ForEach(0..<array.count){number in
    12. Text(self.array[number])
    13. }
    14. }
    15. }
    16. }
    17. #if DEBUG
    18. struct test_Previews: PreviewProvider {
    19. static var previews: some View {
    20. test()
    21. }
    22. }
    23. #endif

    一般来说,我是 SwiftUI 和 GUI 编程的新手,感觉每个内容都是在启动时定义的,之后很难进行更改(例如:在用户导航离开然后返回后重置 View ) .非常感谢循环问题的解决方案或使 View 更具动态性的任何提示!

    最佳答案

    Beta 5 发行说明说:

    The retroactive conformance of Int to the Identifiable protocol is removed. Change any code that relies on this conformance to pass .self to the id parameter of the relevant initializer. Constant ranges of Int continue to be accepted:

    1. List(0..<5) {
    2. Text("Rooms")
    3. }

    However, you shouldn’t pass a range that changes at runtime. If you use a variable that changes at runtime to define the range, the list displays views according to the initial range and ignores any subsequent updates to the range.

    您应该更改 ForEach 以接收一个数组,而不是范围。理想情况下是 Identifiable 数组,以避免使用 \.self。但根据您的目标,这仍然有效:

    1. import SwiftUI
    2. struct ContentView : View {
    3. @State var array:[String] = []
    4. @State var label = "not pressed"
    5. var body: some View {
    6. VStack{
    7. Text(label).onTapGesture {
    8. self.array.append("ForEach refreshed")
    9. self.label = "pressed"
    10. }
    11. ForEach(array, id: \.self) { item in
    12. Text(item)
    13. }
    14. }
    15. }
    16. }

    或者按照rob mayoff的建议,如果您需要索引:

    1. struct ContentView : View {
    2. @State var array:[String] = []
    3. @State var label = "not pressed"
    4. var body: some View {
    5. VStack{
    6. Text(label).onTapGesture {
    7. self.array.append("ForEach refreshed")
    8. self.label = "pressed"
    9. }
    10. ForEach(array.indices, id: \.self) { index in
    11. Text(self.array[index])
    12. }
    13. }
    14. }
    15. }

    关于swift - 如何在数组大小更改后刷新 ForEach 显示元素的数量(SwiftUI、Xcode 11 Beta 5),我们在Stack Overflow上找到一个类似的问题: swift - How to refresh number of ForEach's displaying elements after array's size changes (SwiftUI, Xcode 11 Beta 5) - Stack Overflow

     

    具有非恒定范围视图刷新的Swift ForEach

    swift view swiftui-foreach

    我知道这是一个简单的问题,但我还没有找到答案。我想了解基本概念。

    我试图用非常量范围更新ForEach,closing参数是分配给按钮的变量。

    变量被赋予@状态,因此应该刷新视图。不知怎么的,它不起作用了。

    1. import SwiftUI
    2. struct ContentView: View {
    3. @State private var numberOfTimes = 5
    4. let timesPicker = [2,5,10,12,20]
    5. @State private var tableToPractice = 2
    6. enum answerState {
    7. case unanswered
    8. case wrong
    9. case right
    10. }
    11. func listRange(){
    12. }
    13. var body: some View {
    14. NavigationView{
    15. HStack{
    16. VStack{
    17. Form{
    18. Section {
    19. Picker("Tip percentage", selection: $numberOfTimes) {
    20. ForEach(timesPicker, id: \.self) {
    21. Text($0, format: .number)
    22. }
    23. }
    24. .pickerStyle(.segmented)
    25. } header: {
    26. Text("How many times do you want to practice?")
    27. }
    28. Section{
    29. Stepper("Table to practice: \(tableToPractice.formatted())", value: $tableToPractice, in: 2...16 )
    30. }
    31. Button("Start Now", action: listRange).buttonStyle(.bordered)
    32. List{
    33. ForEach(0..<numberOfTimes){
    34. Text("Dynamic row \($0)")
    35. }
    36. }
    37. }.foregroundColor(.gray)
    38. }
    39. }
    40. }
    41. }
    42. }
    43. struct ContentView_Previews: PreviewProvider {
    44. static var previews: some View {
    45. ContentView()
    46. }
    47. }

     发布于 1 年前

    ✅ 最佳回答:

    avatar

    问题是没有确定范围。让我们排几行

    1. struct Row: Identifiable {
    2. let id = UUID()
    3. }

    然后设置一组可识别的项目

    1. @State private var numberOfTimes = 5
    2. @State private var rows = Array(repeating: Row(), count: 5)

    现在,您可以获得响应列表

    1. List{
    2. ForEach(rows) { row in
    3. Text("Dynamic row")
    4. }
    5. }

    调用更改时更新以重新创建阵列

    1. .onChange(of: numberOfTimes) { newValue in
    2. rows = Array(repeating: Row(), count: newValue)
    3. numberOfTimes = newValue
    4. }

    应在表单上调用onChange。

    当您能够更好地查看模型数据时,这将更有意义,有关更深入的示例,请参阅apple文档。

    这是针对lazy v stack的,但我考虑的是数据模型设置

    https://developer.apple.com/documentation/swiftui/grouping-data-with-lazy-stack-views

    最终解决办法:将FOREACH的列表改为@Published属性即可。

  • 相关阅读:
    QT/自定义槽和信号
    提升珠宝管理效率的新零售行业RFID应用解决方案
    MySql 数据库【表】
    【Docker仓库】使用华为云SWR容器镜像仓库服务
    轻量且强大的 uni-app http 网络库 - 掘金
    5 Spring ApplicationListener 扩展篇
    Go语言函数底层实现
    一文知晓Linux文件权限
    NumPy简单学习(需要结合书本)
    ubuntu 清理缓存
  • 原文地址:https://blog.csdn.net/weixin_42610770/article/details/132053728