I've got a MacOS app I'm converting to use SwiftUI. The UI uses NSOutlineView. Is there a SwiftUI equivalent for this?
Apple Recommended
which allows code like this to be written:
Code Block swift struct FileItem: Hashable, Identifiable, CustomStringConvertible { var id: Self { self } var name: String var children: [FileItem]? = nil var description: String { switch (children) { case nil: return "📄 \(name)" case .some(let children): return children.count > 0 ? "📂 \(name)" : "📁 \(name)" } } } let data = FileItem(name: "users", children: [FileItem(name: "user1234", children: [FileItem(name:"Photos", children: [FileItem(name: "photo001.jpg"), FileItem(name: "photo002.jpg")]), FileItem(name:"Movies", children: [FileItem(name: "movie001.mp4")]), FileItem(name:"Documents", children: []) ]), FileItem(name: "newuser", children: [FileItem (name: "Documents", children: []) ]) ]) OutlineGroup(data, children: \.children) { item in Text ("\(item.description)") }
-
IMO opinion Apple should not recommended as a solution for an outline view, as only partially provides what a an outline view does: just indentation/disclosure, not columns et al. Apple, please provide a built in SwiftUI component...
-
OutlineGroup is a joke IMHO, it neither has the style nor the featureset of a real
NSOutlineView
.
Replies
AFAIK, you have to build it:
h ttps://twitter.com/schwa/status/1136498361110896641
I'm working through this same exercise at the moment...
So far I've found this one of the better articles to read - short and concise - https://dev.to/hugh_jeremy/adding-an-nstableview-to-a-swiftui-view-212p
Seems we need to create a wrapper with
NSViewControllerRepresentable
This is a work-in-progress for me...
which allows code like this to be written:
Code Block swift struct FileItem: Hashable, Identifiable, CustomStringConvertible { var id: Self { self } var name: String var children: [FileItem]? = nil var description: String { switch (children) { case nil: return "📄 \(name)" case .some(let children): return children.count > 0 ? "📂 \(name)" : "📁 \(name)" } } } let data = FileItem(name: "users", children: [FileItem(name: "user1234", children: [FileItem(name:"Photos", children: [FileItem(name: "photo001.jpg"), FileItem(name: "photo002.jpg")]), FileItem(name:"Movies", children: [FileItem(name: "movie001.mp4")]), FileItem(name:"Documents", children: []) ]), FileItem(name: "newuser", children: [FileItem (name: "Documents", children: []) ]) ]) OutlineGroup(data, children: \.children) { item in Text ("\(item.description)") }
-
IMO opinion Apple should not recommended as a solution for an outline view, as only partially provides what a an outline view does: just indentation/disclosure, not columns et al. Apple, please provide a built in SwiftUI component...
-
OutlineGroup is a joke IMHO, it neither has the style nor the featureset of a real
NSOutlineView
.