Questions tagged [swiftui-foreach]

120 questions
23
votes
2 answers

SwiftUI Error: "Closure containing control flow statement cannot be used with function builder 'ViewBuilder'"

I tried a SwiftUI tutorial, "Handling User Input". https://developer.apple.com/tutorials/swiftui/handling-user-input I tried implementing it with for instead of ForEach. But an error arose: "Closure containing control flow statement cannot be used…
10
votes
0 answers

Why does ForEach initialize its Child View twice as much in SwiftUI?

I came across a weird behaviour of SwiftUI's ForEach view. I've noticed that ForEach always initialize its child view twice as much as it should according to its repetition. Normally, when you render the view, its init is called once. But if you put…
Hollycene
  • 287
  • 1
  • 2
  • 12
9
votes
1 answer

searchable modifier not displaying search bar below navigation bar title

I'm using iOS 15 and trying out the new searchable modifier on Lists in SwiftUI. It looks like when you attach searchable(text: $searchText) to a NavigationView the search bar renders on the screen by default below the navigation bar title. When…
Petesta
  • 1,623
  • 3
  • 19
  • 29
7
votes
2 answers

How to update SwiftUI's ForEach in a TabView with PageTabViewStyle modifier when inserting a new element to the collection at first index

This is a very specific issue when using ForEach in a TabView with PageTabViewStyle modifier. Every time I'm inserting an element at the beginning of my array my app crashes. I'm getting a attempt to delete item 11 from section 0 which only…
Marco Boerner
  • 1,243
  • 1
  • 11
  • 34
7
votes
1 answer

How to apply strikethrough() to a tapped foreach Text item in SwiftUI?

I'm trying to apply a strikethrough to any tapped Text items in a ForEach loop: struct BrandListView: View { @ObservedObject var list: ListObject var body: some View { ScrollView { VStack…
Super Noob
  • 750
  • 8
  • 17
6
votes
0 answers

SwiftUI CPU High Usage on Real-Time ForEach View Updating (macOS)

So I have a view and I am computing the amplitude of an audio file and then passing it to a view to graph it and it changes in real-time. The problem is that the graph gets so laggy after few seconds and CPU usage gets maximized and literally…
arata
  • 859
  • 1
  • 8
  • 23
6
votes
2 answers

What does "\.self" actually do in swift/swiftUI?

I don't fully understand what exactly \.self does in the following code: struct ContentView: View { @State private var numbers = [Int]() @State private var currentNumber = 1 var body: some View { VStack { List { …
Super Noob
  • 750
  • 8
  • 17
4
votes
2 answers

Same ForEach loop twice in one SwiftUI View

When I use a ForEach loop over an array twice within a view, I get the following warning at runtime: LazyVGridLayout: the ID 84308994-9D16-48D2-975E-DC40C5F9EFFF is used by multiple child views, this will give undefined results! The reason for this…
D. Mika
  • 2,577
  • 1
  • 13
  • 29
3
votes
1 answer

SwiftUI: Move rows in nested ForEach

I am trying to implement the ability to move rows in a hierarchical SwiftUI List via drag+drop. My List is build with recursive ForEach loops: import SwiftUI struct FileItem: Hashable, Identifiable, CustomStringConvertible { var id: Self { self…
DaPhil
  • 1,509
  • 4
  • 25
  • 47
3
votes
0 answers

SwiftUI: Error message when using .onMove on List built with forEach

I have an array containing some data that I want to present in a View. It should be possible to rearrange the data by holding and dragging it to its new position. I have used the .onMove modifier on a List generated using ForEach to iterate over…
3
votes
3 answers

SwiftUI throwing 'Fatal error: Index out of range' when adding element for app with no explicit indexing

Why, in the following app when clicking through to 'Nice Restaurant' and trying to add a contributor, does the app crash with the error: Swift/ContiguousArrayBuffer.swift:575: Fatal error: Index out of range? The error, in the Xcode debugger, has no…
Jared Khan
  • 333
  • 2
  • 12
3
votes
1 answer

Binding in SwiftUI ForEach from Parent to Child

I have a Subtask defined like this: struct Subtask: Identifiable{ var id = UUID().uuidString var text = "" } I populate an array of them in an ObservableObject like this: //View Model class SubtaskModel: ObservableObject { static let shared =…
Clifton Labrum
  • 13,053
  • 9
  • 65
  • 128
3
votes
0 answers

SwiftUI list: NSInternalInconsistencyException / invalid number of rows

I have a view CountriesListScreen, to which I am passing a list of items. struct CountriesListScreen: View { var listItems: CountriesListItems var body: some View { VStack { Section(header: CountriesListHeader()) { ForEach…
Daniele B
  • 19,801
  • 29
  • 115
  • 173
2
votes
1 answer

Using a ForEach loop to show the key and value of dictionary items in a Form view in SwiftUI

I'm trying to create what I thought would be a simple bit of code to show the key of a dictionary item and implement a Stepper or a Picker to modify the key's value in a single line within a Form view. At the end of the day, all I'm trying to…
2
votes
1 answer

Is it possible to reload only modified item in SwiftUI List/ForEach that uses @Binding

Simple sample code with toggle button (slightly modified from hackingwithswift: This code(hackingwithswift original and my version) IS redrawing every list cell whenever any toggle happens. I modified code to better debug view drawing. import…
Oridadedles
  • 831
  • 1
  • 6
  • 8
1
2 3 4 5 6 7 8