|
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 AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.