我在 SwiftUI 中有一个像这样的 ForEach:
ForEach(entries) { (e: MyType) in
NavigationLinkItem(entry: e)
}
Run Code Online (Sandbox Code Playgroud)
现在我还需要将上一个条目传递给我的视图(NavigationLinkItem)来执行如下操作:
ForEach(entries) { (e: MyType) in
NavigationLinkItem(entry: e, sucessor: ...)
}
Run Code Online (Sandbox Code Playgroud)
如何实现这一点?
为了保持ForEach动态,找到以下可能的方法(假设MyType是Identifiable,并且后继者是可选的)
ForEach(Array(entries.enumerated()), id: \.1.id) { (index, e) in
NavigationLinkItem(entry: e, sucessor: (index == 0 ? nil : self.entries[index - 1]))
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1322 次 |
| 最近记录: |