|
Dear all, I would like to know how you handle Sections in a List in SwiftUI when you use an Currently, I use a function to get the sections titles in my list and filter my results for the section in the second loop.
So how do you handle this case? |
|
Here's one way to get it to work. This uses a simple model with one entity,
Edit: My original solution didn't work so well when adding and deleting items. Not sure why, but I think it had to do with using a Dictionary in the ForEach. Anyway, here's an updated solution that doesn't crash when adding/deleting. |
|
Hi @roosterboy, thank you for your suggestion. The solutions works. What I found is it seems a little slower than my solution. Currently, I have over 1000 items and there is a slighly longer delay in presenting the list. I will come back to you and will report after I tested more than I thought my approach is more time consuming. Additionally, I added searching with searchable which I wasn't able to get working in your code. My ViewModel looks almost the same as yours besides the names and the types. These are my functions to get the data back to my view:
|
|
Thank you again for your help, @roosterboy. I stayed with my solutions for now because I was struggling to implement the search bar with your solution. Speed wise there was no measurable difference with my amount of data. |
SPONSORED You know StoreKit, but you don’t want to do StoreKit. RevenueCat makes it easy to deploy, manage, and analyze in-app subscriptions on iOS and Android so you can focus on building your app.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.