|
Hello, I am stuck on the following problem and hope you can help me. I have a view in which I display a list containing the contents of a class. The class contains objects of a structure that consists of two properties: a date and a value. Struktur:
Klasse
View (... not complete View)
The input is then realized via a further view. Here, date and value are entered and added to the class. So far it works quite well. Now to my question/problem. I would like to sort the list of items by date. I have now come up with the following: In the View / List / ForEach
This also works at first sight, but not at second, because if I delete rows now everything gets messed up, because probably the function removeItems and IndexSet doesn't work anymore. Is there a way to solve this in the removeItems function or do I have a bug here in general regarding sorting the list/items? I am grateful for any advice! Regards, Ralf |
|
You just need to match up the item(s) in the sorted array that you want to delete with the item(s) in the original array. Use the Hope that makes sense. No code because I'm typing this on my phone while lying in bed. :) If you search the forums for the iExpense project, I believe that's the one that has an example of this sort of thing and you can see how others have solved the same problem. |
|
Hi @roosterboy, thanks for the hint, I will see if I find a solution using firstIndex(where:) .. never used it before. I'm coming/learning from 100days of swift and the little app I'm trying now was based on the stuff I've learned from iExpense project, but unfortunately I wasn't able to find something in the forum that helps to find a solution. If you find some time later on, maybe you can provide some code snipped for me .. or if I find a solution I will post it here. |
|
Hi, I have not found a working solution yet, but I would like to know if my theoretical understanding is correct.
If I have understood this correctly, I am still missing the brilliant idea for the closure ... ?? Any comments are welcome :-) |
|
I've tried a few things and put in a few print statements, but still don't understand how to get this together .... ?? I have extended the removeItems function with print statements to 1. output all items in the original array and 2. the selected entry
I have deleted item from "date: 2023-02-10", which was the 3rd item in the list, and here is the output from the console:
The selected row is index 3 in the array and not index 2. I currently have no idea how to map the row I select to delete in the sorted list with the entry in the original array. |
|
Hi @Obelix, first of all thank you very much for the helpful and especially instructive hints about my code! This helps me a lot, because I'm still very much at the beginning and sometimes think I'll never make it ... First I undid all my previous changes regarding sorting and then tried to incorporate your hints. The sorting works very well and is now, due to the Comparable in the MeterReadingItem Struct, very clear:
In the view I now only have the following line in the ForEach:
Unfortunately, I can't get anywhere with the removeMeterReading function in the MeterReadings180 class. I don't know how to get the UUID of the entry marked as to be deleted by the user? In the .delete modifier I seem to get only the selected index, but I still need the UUID. In my .onDelete modifier, I call the following:
My removeMeterReading function now looks like this:
Could you help me how to code the .delete modifier in the view to call the removeMeterReading function with the correct selected UUID? |
|
As i understood from conversation there is a mismatch between two arrays. So you have array in your model with @Published wrapper, which you use in your view but sorted. Why not to use sorted() upon init() of that array in your model. So meaning you already have it sorted and no need to use sorting method on ForEach. And in this case you have no mismatch between the model and view so indexes are the same... So you can use your function as it was i.e.
|
|
May I offer two solutions without further confusing you with all the options offered here. I tried to use your code as much as possible so that you can understand what is going on. Option 1: You sort the array before using it in your view so that it knows the positions of each item and delete them in the right way
Option 2: More elegant than above but some changes to be made in your code.
There are many other options to do the same, but all depends on the logic in your workflow. |
|
@Obelix, @ygeras, Many thanks for your support! Most important for me is the fact that you teach me how what works and what possibilities of the solution one has. This is very helpful for me as a beginner! I have looked at your ideas and suggestions and tried to understand and implement it for me. My first important realization, which I've been failing at repeatedly from my understanding over the past few days, the .onDelete modifier does not have access to my "item" object in my list. This is where I get an IndexSet. If I stick to the .onDelete I would use the suggested solution 1. from @ygeras and sort already in the class at init(). Since it is more logical and clearer for me, with the Comparable structure of my items, I tried to implement the ideas of @Obelix and @ygeras and then came for me to the following solution. This seems to work as desired: The following function to delete the items in the MeterReadingItems180 class:
In the View (extract, not complete)
I will take the approach of @ygeras and do the sorting at the beginning of the structure and then give the sorted array into the view. The conclusion for me: There is still so much to learn and sometimes I think that it exceeds my horizon, on the other hand it is a lot of fun and thanks to your help you can get further even if there is apparently no progress :-) |
SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
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.