I’ve hit a strange SwiftUI preview crash that happens on macOS previews when using a view inside a List’s ForEach, resulting in the error Fatal Error in TableViewListCore_Mac2.swift. Only crashes macOS preview - iPhone/iPad preview doesn't crash. Doesn't crash when actually running the app. Here’s a minimal reproducible example, causing the preview to crash. XCode: Version 26.0.1 (17A400) MacOS: 26.0.1 (25A362) import SwiftUI struct Item: Identifiable { let id = UUID() let name: String } struct ItemRow: View { let item: Item var body: some View { HStack { Button(action: {}) { Image(systemName: play) } Text(item.name) Spacer() ProgressView() } } } struct ContentView: View { @State private var items = [ Item(name: Item A), Item(name: Item B), ] var body: some View { List { ForEach(items) { item in ItemRow(item: item) } } } } #Preview(Content view) { ContentView() } #Preview(Item row) { ItemRow(item: Item(name: Item A)) } If I wrap the row in a container, like this: ForEach(it
Search results for
SwiftUI List performance
50,605 results found
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I'm using ScrollView to display my course list, and each card in the list, named 'CourseCardView' as shown in the code. When I test it on both the simulator and a physical device, I notice that scrolling is not smooth and feels laggy. I'm not sure how to address this issue. Here is my code: // // CourseListView.swift // LexueSwiftUI // // Created by bobh on 2023/9/3. // import SwiftUI struct CourseCardView: View { let cardHeight: CGFloat = 150 let cardCornelRadius: CGFloat = 10 let cardHorizontalPadding: CGFloat = 10 @State var courseName = course name @State var courseCategory = category name @State var progress = 66 var body: some View { ZStack { Image(default_course_bg2) .resizable() .blur(radius: 5, opaque: true) .cornerRadius(cardCornelRadius) .padding(.horizontal, cardHorizontalPadding) .frame(height: cardHeight) Color.white .cornerRadius(cardCornelRadius) .padding(.horizontal, cardHorizontalPadding) .frame(height: cardHeight) .opacity(0.1) VStack(alignment: .leading, spacing:
In this part of the tutorial on line 5 I get an error that states Landmark does not conform to protocol Encodable also does does not conform to protocol 'Decodable', 'Equatable' or 'Hashable'. I can not get past this part in the tutorial. Also on line 29 I get an error can't find type 'Coordintates' in scope. Anyone have any answers. Thank you.
Hello, I am using a Double Column Layout on the iPad. I am trying to highlight the first row on the view loading. I was able to get the first row navigation link to load using selection and tags. I cannot figure out how to get the first row to highlight on the left as if it was tapped. Guidance would be great! Thanks in advance!
I'm targeting iPad ios 14, with this test code: struct ContentView: View { tt tttt@State var arr1: [String] = [one, two, three] tttt@State var arr2: [String] = [four, five, six] tt ttvar body: some View { ttttList { ttttttSection(header: Text(first section)) { ttttttttForEach(arr1, id: .self) { s in ttttttttttText(s) tttttttt} tttttt} ttttttSection(header: Text(second section)) { ttttttttForEach(arr2, id: .self) { s in ttttttttttText(s) tttttttt} tttttt} tttt} tt} tt} Is there a way to make the section headers display what I put in the Text, not change it to uppercase?
When moving a row in a SwiftUI List I note that due to my save Core Data call the UI transition is jerky. I call save against core data as the list items orderNum has changed, so I'm saving that change that. Video and code below. If I remove the GCCoreData.save() there is no stutter.Animated GIF: https://i.stack.imgur.com/lsmtV.gifCode - Extract from my SwiftUI View private var tasksFetchRequest : FetchRequest private var gcTasks : FetchedResults { tasksFetchRequest.wrappedValue } init(withGcList gcList:GCList) { self.currentList = gcList self.tasksFetchRequest = FetchRequest( entity: GCTask.entity(), sortDescriptors: [NSSortDescriptor(key: orderNum, ascending: true)], predicate: NSPredicate(format: gcList == %@, currentList) ) } private func updateOrderNums(startIndex: Int, destIndex: Int, inc: Int) { var currIndex = startIndex while currIndex <= destIndex { gcTasks[currIndex].orderNum = gcTasks[currIndex].orderNum + Int64(inc) currIndex += 1 } } fileprivate func moveRo
I am making this calculator where one option would be selected for each radio button, the question is how to implement code to perform calculation depending on each option. I ma newbie and was able to finally make these multiple radio buttons on screen that grouped together. For example, if height is in inches, need to first change height to cams, and considering following code, but unable to ` var heightCms: Double = 0.0 if (heightRadioButtonGroups(callback: heightRadioInchesMajority).isSelected) { return Double(height) ?? 1 * 2.54 } else { return Double(height) ?? 1 } */ Though heightRadioInchesMajority is within the code, but getting error that is out of scope. Would appreciate a simplified solution. In Android Kotlin, it was easy and was able to do it quickly, but here finding difficult and most of online videos are using Ukit or older versions and not much on swiftUI. In the code shows I used 1 for default value but doesn't matter if it is 0 or 1. I used 1, so that multiplying with zero
Please review Demystify SwiftUI performance WWDC session. It covers the topic extensively.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags:
I need to execute a code before the device's interface starts rotating but I can't seem to find any viable solution. I came across UIDevice.orientationDidChangeNotification but couldn't find anything like that to be notified that an orientation change will start. I tried creating an UIViewControllerRepresentable with a custom UIViewController that overrides func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator), but that function doesn't get invoked at all (I didn't forget to propagate it using super). Any idea?
Hey! This is expected but let me explain why this is happening and how you can stay on the fast path. SwiftUI needs to know the total number of rows in the List upfront. This can be done very efficiently if SwiftUI knows statically that each View returned by ForEach returns a constant number of children. if,other conditional construct or AnyView make this number dynamic. This requires SwiftUI to eagerly iterated over all elements, create those views and compute how many rows this ForEach actually produces. And it needs to do this on every change. If the number of rows per view returned by ForEach is instead know statically, e.g. if conditionals are wrapped in an HStack or have only unconditional concrete views, SwiftUI can simply multiple this static number with count of data given to ForEach. SwiftUI needs to know the number of rows for multiple reasons but the scroll indicator is one example. This is very similar to UICollectionView or NSTableView. I hop
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags:
I have a List and about 1000 item of Array, and my item update realtime (100 messenger updated/s). My List is very very lag. I see ForEach run full (1000 turn)/ (1 item update) => crazy Can you help me a solution like a reloadRows in UIKit with UITableView. Thank so much! This my test code (I try with List and LazyVStack): struct PriceBoardView2 : View { @EnvironmentObject var loadingEnv : LoadingEnv @State var listUser : [UserModel] var body: some View { VStack(content: { Button(action: { updateData() //fake data realtime update }, label: { Text(Fake data realtime update) }) List(content: { ForEach(listUser.indices, id: .self) { i in RowPriceBoardView2(userModel: listUser[i]) } }) // ScrollView(content: { // LazyVStack(content: { // ForEach(listUser.indices, id: .self) { i in // RowPriceBoardView2(userModel: listUser[i]) // } // }) // }) }) .onAppear(perform: { for i in 0..<1000 { listUser.append(UserModel(number: i, name: -, age: 0)) } }) } func updateData(
Hello 👋 I ran into a SwiftUI lifecycle gotcha while debugging a List with .refreshable I share the code used to reproduce the issue @Observable final class CounterModel: Identifiable { let id: String var title: String var value: Int init(id: String, title: String, value: Int = 0) { self.id = id self.title = title self.value = value } deinit { print(deinit, title) } } @Observable final class ObservableCountersStore { var counters: [CounterModel] = [ .init(id: 1, title: A), .init(id: 2, title: B), .init(id: 3, title: C) ] func refresh() async { try? await Task.sleep(nanoseconds: 300_000_000) counters = [.init(id: 4, title: D)] } } struct ObservableCountersListView: View { @State private var store = ObservableCountersStore() var body: some View { List { ForEach(store.counters) { counter in ObservableCounterRow(counter: counter) } } .refreshable { await store.refresh() } } } struct ObservableCounterRow: View { let counter: CounterModel var body: some View { Text(counter.title) } } Obse
Lists seems to be lazy on iOS but not on macOS 12. It would be great to see lazy lists in macOS 13. There is also a discussion about List performance on macOS on StackOverflow.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags:
And in general, it feels like combining Grid and List is not a good idea and they should be used separately. ForEach isn't a List, it only provides views based on a RandomAccessCollection of some data type. However a List, a Grid, or a stack can be configured using a ForEach. See Editing Grids to learn how to organize and edit grids. A List with a ForEach that’s configured with the onDelete(perform:) or onMove(perform:) modifier will provides controls to delete or move list items while in edit mode. See EditButton for examples on how to reorder a List.
Topic:
UI Frameworks
SubTopic:
SwiftUI
@JbXyz Please review Demystify SwiftUI performance WWDC session. It covers the topic extensively.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: