Learn Courses My Dashboard

Swipe on list view items

I have a TextField and a swappable list view. I wanted to copy the text value of item swiped to the state variable binded to the TextField, so that if I click the edit button, I don’t need to retype all the content of the item selected/swiped

but currently, I am only allowed to define buttons to display on swipeAction and define what will happen when that button is pressed

but what I need is to define what will happen between the item is swiped and before a button is pressed

sample codes:

List(problems) { p in
                    
                    // maybe contain the text on zstack rectange?? so the Text boundary will expand?
                    Text(p.content ?? "")
                        .swipeActions(allowsFullSwipe: false) {
                            // *** I want to inset here to update a state variable on swipe***
                           //


                            Button {
                                viewContext.delete(p)
                                
                                do {
                                    try viewContext.save()
                                    
                                } catch {
                                    
                                }
                                
                            } label: {
                                Label("Delete", systemImage: "minus.circle")
                            }
                            .tint(.red)
                            
                            Button {
                                let str = problemValue.trimmingCharacters(in: .whitespacesAndNewlines)
                                p.content = str
                                do {
                                    try viewContext.save()
                                    
                                } catch {
                                    
                                }
                                problemValue = ""
                                
                                
                            } label: {
                                Label("Edit", systemImage: "pencil.circle")
                            }
                            .tint(.blue)
                        }
                    
                }

I also tried to use Gesture → DragGesture, but it only executes when the text it self is dragged and not executed when space around the text is dragged, also with this, the swipeAction no longer execute when drag is performed on text.

Given that you have a List (or a table if you like) of “problems” then you can identify the row (“problem”) so with that in your Button action you should be able to store the “problem” you are about to delete.

What I mean is that just before viewContext.delete(p) save that p somehow.

Do you have a ViewModel that looks after your data as an ObservableObject?

Hi Chris,

Thank you for your reply. Currently I am doing the Module 5 - Core Data.
In my understanding of Module 5 - Lesson 2, the view model and the model was autogenerated by CoreDataPrototype->Create NSManageObject Subclass… in editor Menu.

But Anyway, the effect that I really want to do is:

Currently I have this:

then if swipe the list, I want to copy the swiped list’s text to the Add Problem Text Field.
So that if the user wanted to edit the current selected item, they don’t need to write the whole text all over again.

but the problem is, when using swipeActions, I am only allowed to define the buttons and that buttons action. And I am not allowed to define what will happen the moment the user swipe the text other than display the edit and delete buttons.

Something like this:

or this:

If I understand you correctly you want to intercept the swipe action to ask the user if he/she wants to copy that text before the swipe action takes effect? Ie, you don’t want to copy the text by default?

(edit)
OK, I see what you are trying to do. No you can’t add code like that because the code needs to return a View

Place that line of code in the Button action closure immediately before viewContext.delete(p) like this:

Button {
    problemValue = p.content
    viewContext.delete(p)

    do {
etc
etc
...
1 Like

I see… Actually, I have the edit button also…

So when user click the Blue Edit button,
I will reflect the value on the text field and at same time I will replace the + Add button to be “Update”
Then the user will do its edit on text and pressed the “Update” button when finish editing to execute the contextView.save()

I think it’s possible, I just need to do extra code to pullout the p from the list to outside method.

Though it could have been better if I can implement it 2 steps rather than 3 steps and confine the area of control in smallest place as possible rather than spread out (Edit and delete)

But I guess, for now there is no choice. Or maybe, I will reconsider the UI/UX design.

But Thank you. I appreciate your inputs.

1 Like