UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Edit Core Data list items in place (extending Hacking with macOS Bookworm project)

Forums > macOS

@rjriv  

Hello,

Could anyone point me in the right direction in order to edit Core Data list items "in place" similar to how you can rename your files and folders in the Xcode files list by clicking on it after selecting it and then the list item changes into an editable text field?

For example, in the Hacking with macOS Bookworm project, we create a sidebar with a list of book names and selecting the item in the list changes the detail view to reflect that book. And while we can edit the name in the detail view and it will be reflected in the list view, I am curious to know how in place editing can be achieved.

Thank you!

3      

What kind of view are you using for the list items?

If you use a TextField for the list item, you should be able to change the name of the book from the sidebar.

3      

@rjriv  

Well, the current non-editable list looks like:

        List(reviews) { review in
            NavigationLink(tag: review, selection: $selectedReview) {
                DetailView(review: review)
            } label: {
                Text(review.reviewTitle)
            }

But

  1. I can't seem to create a binding to the review in the list in order to use a TextField like I was using this syntax in the following test code:
        List($dataSource.items) {$item in
            TextField(item.name, text: $item.name)
        }

I imagine it is because "reviews" is defined as follows:

    @FetchRequest(sortDescriptors: [SortDescriptor(\.id)]) var reviews: FetchedResults<Review>

And

  1. The example in #1 above showed me that the items in the list can no longer be selected, but just edited when you click on them. This makes me think you need to somehow change the item in the list from a Text to a TextField when you click on a selected item or something.

Thanks...it seems like it should be straightforward since it is done in so many applications, but maybe it isn't!

3      

Apple has changed the standard behavior in a recent macOS update. I have the following project on GitHub:

WikiDemo

The Mac version used to work properly. Now it shows the same behavior you're experiencing: clicking on a list item lets you change its title but won't let you select the item. Now you have to either use the up and down arrow keys or double-click the item to select it, which seems like odd default behavior to me. It makes more sense to have single-click select the item and double-click edit the item.

3      

@rjriv  

Thanks for trying to point me in the right direction! I can see from your demo that at least you can click outside the text area to select a row...I was too quick and didn't implement the selected row feature in my test, so that's better than nothing.

However, I can't get it to work at all on top of a core data fetched request result set, so I'll have to keep looking.

Thanks again!

3      

Hacking with Swift is sponsored by RevenueCat

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.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.