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

Can table or list rows be disabled in SwiftUI?

Forums > macOS

I have some data I want to display in a table (in macOS) and a list (in iOS) where certain rows were created elsewhere in the app (and any editing & deleting needs to be done from there), and other rows that were created within the table or list itself (that can be freely edited or deleted). There is a boolean in the data that is true (if the row contents were created outside the current table or list) and false (if they were created in the table or list).

I know I could use use a foreground color that changes on the rows based on the boolean for that row and then trap for things like delete and selection if the boolean is true for the selected line. But before I do that, I was wondering if there is any way to simply disable the lines in the table or list where the boolean is true so they just can't be selected or deleted to begin with? I thought about .disabled() but I'm not seeing any way I could use that in this context.

                    Table(tableData, selection: $selectedLine) {
                        TableColumn("Name") { sampleLine in
                            Text(sampleLine.name)
                                .foregroundColor(sampleLine.disabled ? .secondary : .primary)
                        }
                        TableColumn("Disabled") { sampleLine in
                            Text(sampleLine.disabled ? "true" : "false")
                                .foregroundColor(sampleLine.disabled ? .secondary : .primary)
                        }
                    }

3      

Decided to just go with the appearance of disabling (with foreground color) and using alerts to let the user know why they can not delete or edit certain rows of the table or list. More work to set up, but worth it. 🙂

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

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.