【问题标题】:Can a Swiftui Identifiable Foreach loop select only the first 5 itemsSwiftui Identifiable Foreach 循环能否只选择前 5 个项目
【发布时间】:2021-11-25 07:54:20
【问题描述】:

你好,我有一个项目只需要在 30 多个项目的循环中显示前 5 个项目,下面是我的代码

struct Introductions: Codable, Identifiable {
   let id: String
   let topIntros: String?
   let image: String
   let date: String
}

ForEach(introductions) { introduction in
   NavigationLink(destination: IntroductionDetailView(introduction: introduction)) {
      IntroductionsView(introduction: introduction)
   }
}

我尝试使用此方法,但 xcode 在我滚动超过第五项后崩溃了

ForEach(introductions, id: \.topIntros) { introduction in
   NavigationLink(destination: IntroductionDetailView(introduction: introduction)) {
      IntroductionsView(introduction: introduction)
   }
}

谢谢

【问题讨论】:

  • 这不是我们在这里展示代码的方式。
  • 我知道我正在寻找格式化程序,但以正确的方式呈现代码还没有运气
  • 终于搞定了 Ctrl K
  • var body: some View { List { ForEach(introductions.startIndex..
  • 感谢您的回复,但在这种情况下我更喜欢使用 LazyVStack,我没有完全理解您的信息

标签: foreach swiftui identifiable


【解决方案1】:

这里有一个简单的方法:

struct ContentView: View {
    
    @State private var array: [Int] = Array(100...130)

    var body: some View {
        
        if (array.count >= 5) {
            
            ForEach(0...4, id:\.self) { index in
                
                Text(String(describing: array[index]))
      
            }
            
        }

    }
    
}

这里有另一种方式:

struct ContentView: View {
    
    @State private var array: [Int] = Array(100...130)

    var body: some View {
        
        ForEach(array.dropLast(array.count - 5), id:\.self) { item in
            
            Text(String(describing: item))

        }

    }
    
}

【讨论】:

  • 你好@swiftPunk,数据来自 json 文件,所以我不能使用 Int
  • 这是一个帮助你理解的例子!用我的回答解决你的问题
【解决方案2】:

你可以试试这样的:

if (introductions.count >= 5) {
    ForEach(introductions.prefix(upTo: 5), id:\.id) { introduction in
       NavigationLink(destination: IntroductionDetailView(introduction: introduction)) {
          IntroductionsView(introduction: introduction)
       }
    }
}

【讨论】:

    【解决方案3】:

    使用@workingdog 的评论我能够修复它并在下面发布答案,以防它派上用场。

    struct Introductions: Codable, Identifiable {
       let id: String
       let topIntros: String?
       let image: String
       let date: String
    }
    
    ForEach(introductions) { introduction in
       NavigationLink(destination: IntroductionDetailView(introduction: introduction)) {
          IntroductionsView(introduction: introduction)
       }
    }
    
    
    struct ContentView: View {
    
        var body: some View {
            VStack { 
    if (introductions.count >= 5) {
        ForEach(introductions.prefix(upTo: 5), id:\.topIntros) { introduction in
           NavigationLink(destination: IntroductionDetailView(introduction: introduction)) {
              IntroductionsView(introduction: introduction)
           }
        }
    } else {
    ForEach(introductions) { introduction in
       NavigationLink(destination: IntroductionDetailView(introduction: introduction)) {
          IntroductionsView(introduction: introduction)
       }
    }
    }
    }
    
        }
        
    }
    

    使用 .id 破坏了视图层次结构,因此我使用了可选的 .topIntros,结果一切正常。

    【讨论】:

      猜你喜欢
      • 2018-06-23
      • 1970-01-01
      • 1970-01-01
      • 2019-11-21
      • 1970-01-01
      • 2013-11-26
      • 2023-03-08
      • 1970-01-01
      • 2021-03-03
      相关资源
      最近更新 更多