|
Hi, hopefully someone can point me in the right direction with solving this problem. I want to display a table with each row containing some text and integer value. This is all straight forward. What I want now, is a Slider, in each row that will allow the user to control the integer value. My data for the row's are in custom classes, having properties for a UUID and the text and integer values. I can get to the data in an Array form for easy display using List but I can also access elements directly using a dictionary. My problem is, the number of rows is dynamic, the user can add/remove them. How do I setup the value of the Slider and have it reference a particular row's data. In my mind i would like something like @State private var rows: [UUID,Integer] and then in the Slider be able to get the UUID for the specific data row and updates its Integer . . . Any thoughts or idea's how to solve this one. Thanks |
|
UPDATE: I have found a solution here The trick is to use Binding(...) directly in the value property of the Slider. This gives access to the get and set, within the scope of each row, enabling access to my Row data. I can then update my properties. The only issue at the moment is the view is not being re-drawn because no State has changed. Have a hacky boolean flag to force it but will keep looking for a cleaner solution. |
|
Have you tried keeping it simple.....?
Don't forget to make it identifiable, as I know you already know, but anyone else reading this might not! Best of luck |
|
Thanks for the reply @markbooth5457. The problem i had is that I have more the one slider and related $number to display. The number of numbers is dynamic, I don't know how many will be required in advance of displaying the view so defining individual @State for each was not option. The trick I have learned is replacing the traditional $number with Binding. So rather than $number for the Slider value param I have something like this . . .
nodes being my Array of custom data which i loop over with a List in the normal way. I can then identify the record i want to update using an Id value in the object. |
|
Not sure I understand this any better, I'm afraid. Do you mean that each line has more than one number? Has the Binding code sorted out your problem? If so I will try to understand it better so that I can add it to the old toolbox ;-) Thanks |
|
Sure, I'm probaby not explaining very well. I have a class like this . .
The user can add as many of these as they like to the nodes array. Typically 10 to 30 (reasonable max 100), these can be added and removed at any time. My view is displaying these in a List. With each cell/row showing the name, weight and Slider to update the weight value for the specific node. The slider is to control the weight value. So on a typical view i will have 10 to 30 of these Sliders. The Binding code has sorted the issue as it allows me to access and update the weight value in the appropriate Node in my array of nodes and does not require potentially 100+ @State var's :) probably should have started with this explanation . . . |
|
Now I seeeeee! I was thinking of something else, but the following is something similar - perhaps the key part is using a struct instead of a class for the node:
Not sure if this helps with the sortPosition, though. Not quite sure what you meant to do with that. But it only uses one State var for any number of lines, so maybe it fits? |
|
|
|
I hope it is useful. Binding is the old way, and probably underlies how the $ works, but the $ is much cleaner once we manage to work out how it fits in. All best wishes Mark |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
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.